【发布时间】:2019-02-16 22:41:45
【问题描述】:
我正在尝试从黄页中获取数据,但我只需要编号的管道工。但我无法在 h2 class='n' 中获取文本编号。我可以获得 class="business-name" 文本,但我只需要编号的管道工而不需要广告。我的错误是什么?非常感谢。
这个html:
<div class="info">
<h2 class="n">1. <a class="business-name" href="/austin-tx/mip/johnny-rooter-11404675?lid=171372530" rel="" data-impressed="1"><span>Johnny Rooter</span></a></h2>
</div>
这是我的python代码:
import requests
from bs4 import BeautifulSoup as bs
url = "https://www.yellowpages.com/austin-tx/plumbers"
req = requests.get(url)
data = req.content
soup = bs(data, "lxml")
links = soup.findAll("div", {"class": "info"})
for link in links:
for content in link.contents:
try:
print(content.find("h2", {"class": "n"}).text)
except:
pass
【问题讨论】:
标签: python-3.x web-scraping beautifulsoup