我用 python 创建了一个脚本来解析一些 html 元素的地址。当我执行脚本时,我从元素中获取 title
、address
和 phone
号码,而我的目的是仅获取 < em>地址。如果我使用 next_sibling
,我只能获取由 br 标记分隔的地址的第一部分,这就是我跳过该方法的原因。
如何从下面的代码片段中只获取地址而不获取其他内容?
from bs4 import BeautifulSoup
htmldoc = """
<div class="search-article-title-description">
<div class="search-article-title">
<a href="https://www.pga.com/pgapro/info/999918438?atrack=pgapro%3Anone&seapos=result%3A1%3AJeff%20S%20Swangim%2C%20PGA&page=1">Jeff S Swangim, PGA</a>
<div class="search-article-protitle">
Assistant Professional
</div>
</div>
<div class="search-article-address">
<div class="search-instructor-course">
Lake Toxaway Country Club
</div>
4366 W Club Blvd<br>Lake Toxaway, NC 28747-8538<br>
<div class="spotlightphone_num">
(828) 966-4661
</div>
</div>
</div>
"""
soup = BeautifulSoup(htmldoc,"lxml")
address = soup.select_one(".search-article-address").get_text(strip=True)
print(address)
我现在得到的:
Lake Toxaway Country Club4366 W Club BlvdLake Toxaway, NC 28747-8538(828) 966-4661
我的预期输出:
4366 W Club BlvdLake Toxaway, NC 28747-8538
最佳答案
我能想到的最简单的方法是使用.extract()
函数来踢出你不感兴趣的部分。如果我们可以忽略这个类的内容search-instructor -course
和 spotlightphone_num
那么剩下的部分就是所需的部分。
以下脚本应该为我们提供地址:
from bs4 import BeautifulSoup
htmldoc = """
<div class="search-article-title-description">
<div class="search-article-title">
<a href="https://www.pga.com/pgapro/info/999918438?atrack=pgapro%3Anone&seapos=result%3A1%3AJeff%20S%20Swangim%2C%20PGA&page=1">Jeff S Swangim, PGA</a>
<div class="search-article-protitle">
Assistant Professional
</div>
</div>
<div class="search-article-address">
<div class="search-instructor-course">
Lake Toxaway Country Club
</div>
4366 W Club Blvd<br>Lake Toxaway, NC 28747-8538<br>
<div class="spotlightphone_num">
(828) 966-4661
</div>
</div>
</div>
"""
soup = BeautifulSoup(htmldoc,"lxml")
[item.extract() for item in soup.find_all(class_=["search-instructor-course","spotlightphone_num"])]
address = soup.select_one(".search-article-address").get_text(strip=True)
print(address)
关于python - 无法从某些 html 元素中获取文本的特定部分,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54013637/