python - 无法从某些 html 元素中获取文本的特定部分

标签 python python-3.x web-scraping beautifulsoup

我用 python 创建了一个脚本来解析一些 html 元素的地址。当我执行脚本时,我从元素中获取 titleaddressphone 号码,而我的目的是仅获取 < 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&amp;seapos=result%3A1%3AJeff%20S%20Swangim%2C%20PGA&amp;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 -coursespotlightphone_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&amp;seapos=result%3A1%3AJeff%20S%20Swangim%2C%20PGA&amp;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/

相关文章:

仅在 PC 启动时出现 Python 错误 sqlite3.OperationalError : unable to open database file

python - 在 Seaborn 图中指定独立轴值

python-3.x - 检查十进制数的二进制表示是否只存在一个 '1' 的最有效方法

xml - 抓取此 URL、R XML 并获取 sibling

python - Scrapy:如何提取 HTML 标签内的所有单词?

Python ctypes : One byte variable

python-3.x - 导入错误: cannot import name 'pairwise_distances_chunked'

python - 加载在 Python 2 和 Python 3 中计算的 gensim Word2Vec

Python 页面倒计时打印 (len(elem_href1-(number)))

python - 根据内部列表元素的比较从列表列表中删除重复项