2016-04-16 63 views
0

刮饭店网站检索标题和价格。 “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?

+1

你的问题还不清楚。请用预期的输出显示示例HTML文档。 – styvane

+0

更新错误消息和澄清。没有我可以在这里分享的样本数据。 – mutantChickenHer0

回答

3

由于错误消息明确表明,没有find_all()方法,您可以在list对象中调用该方法。在这种情况下,你应该叫find_all()list,而不是单独的部件上,假设你需要从div.hotel-wrap的一些信息以及相应的h3

for hotel in hotelInfo: 
    hotelTitle = hotel.find_all('h3', attrs={'class': 'p-name'}) 

如果你只需要h3元素,你可以结合二个选择器直接得到,而不必查找hotelInfo第一:

hotelTitle = soup.select('div.hotel-wrap h3.p-name') 
0

对于酒店介绍,在hoteltitle拉链(hotelinfos,hoteltitles): 数据= { '酒店介绍':hotelinfo.get_text(), } 打印(数据)

就像那个