我正在尝试从黄页获取数据,但我只需要编号的管道工。但我无法在 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
请您参考如下方法:
您需要一个不同的类选择器来限制该部分
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 = [item.text.replace('\xa0','') for item in soup.select('.organic h2')]
print(links)
.organic
是一个单一的类选择器,来自复合类,用于限制所有编号管道工的父元素。观察突出显示是如何在广告之后开始的:
输出: