刮饭店网站检索标题和价格。 “hotelInfo”是div
,它包含有趣的内容。BeautifulSoup搜索beautifulsoup结果?
对我来说,我只想在div
上执行操作。我的代码如下 -
from bs4 import BeautifulSoup
import requests
response = requests.get("http://$hotelurlhere.com")
soup = BeautifulSoup(response.text)
hotelInfo = soup.select('div.hotel-wrap')
hotelTitle = soup.find_all('h3', attrs={'class': 'p-name'})
hotelNameList = []
hotelPriceList = []
for hotel in hotelInfo:
for title in hotelTitle:
hotelNameList.append(title.text)
它是更有意义的说,hotelTitle应该是在hotelInfo上面的Beautifulsoup搜索。然而,当我尝试这个
hotelTitle = hotelInfo.find_all('h3', attrs={'class': 'p-name'})
错误消息:返回
Traceback (most recent call last):
File "main.py", line 8, in <module>
hotelTitle = hotelInfo.find_all('h3', attrs={'class': 'p-name'})
AttributeError: 'list' object has no attribute 'find_all'
错误这是涉及到列表元素没有“find_all”的属性。我知道这是因为hotelInfo是返回的列表元素。我在正确的方法中搜索了信息,以检查此列表中的h3
信息,但我没有取得任何成功。
这样做的最好方法是什么? 我不应该能够将hoteTitle设置为hotelInfo.find_all而不仅仅是soup.find_all?
你的问题还不清楚。请用预期的输出显示示例HTML文档。 – styvane
更新错误消息和澄清。没有我可以在这里分享的样本数据。 – mutantChickenHer0