我想使用 python 和 bs4 从以下 html 代码中仅提取地名。
<div class="results-list" id="theaterlist">
<table>
<tr class="trspacer">
<td>
<a href="theater.aspx?id=4000642">
<h2 class="placename">
Hyde Park
<span class="boldelement">
Richmond Avenue 56 ls61bz
</span>
</h2>
</a>
我正在使用以下代码,但我也得到了地址。
mydivs = soup.find("div", {"id": "theaterlist"})
lis = mydivs.select("a[href*=theater.aspx]")
for x in lis:
theater = x.find('h2', class_='placename')
print theater.text
如有任何帮助,我们将不胜感激。
最佳答案
要仅获取元素(而不是子元素)的文本,您可以使用.find(text=True)
:
data = """
<div class="results-list" id="theaterlist">
<table>
<tr class="trspacer">
<td>
<a href="theater.aspx?id=4000642">
<h2 class="placename">
Hyde Park
<span class="boldelement">
Richmond Avenue 56 ls61bz
</span>
</h2>
</a>
"""
from bs4 import BeautifulSoup
soup = BeautifulSoup(data, 'lxml')
print(soup.find('h2').find(text=True).strip())
打印:
Hyde Park
关于python - 使用 beautifulsoup4 从 div 获取文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51389843/