[英]BeautifulSoup search on beautifulsoup result?
Scraping a hotel website to retrieve titles and prices. 搜寻旅馆网站以检索标题和价格。 "hotelInfo" is the div
that holds the interesting content. “ hotelInfo”是保存有趣内容的div
。
It makes sense to me that I would want to only perform my operations on this div
. 对我来说,我只想在此div
上执行操作就很有意义。 My code is as follows - 我的代码如下-
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)
It makes more sense to say that hotelTitle should be a Beautifulsoup search on hotelInfo above. 说hotelTitle应该是上述hotelInfo上的Beautifulsoup搜索更有意义。 However when I try this 但是当我尝试这个
hotelTitle = hotelInfo.find_all('h3', attrs={'class': 'p-name'})
Error message: 错误信息:
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'
An error was returned which was related to the list element not having an attribute of "find_all". 返回了与列表元素不具有“ find_all”属性有关的错误。 I understand that this is because hotelInfo is a list element that was returned. 我了解这是因为hotelInfo是返回的列表元素。 I've searched for information on the correct way to check for the h3
info within this list but I am not having any success. 我已经搜索了有关检查此列表中的h3
信息的正确方法的信息,但没有成功。
What is the best way to do this? 做这个的最好方式是什么? Shouldn't I be able to set hoteTitle to hotelInfo.find_all rather than just soup.find_all? 我应该不能将hoteTitle设置为hotelInfo.find_all而不是只设置soup.find_all吗?
As the error message clearly suggests, there is no find_all()
method which you can invoke in a list
object. 如错误消息清楚地表明,没有可在list
对象中调用的find_all()
方法。 In this case, you should call find_all()
on individual member of the list
instead, assuming that you need some information from the div.hotel-wrap
as well as the corresponding h3
: 在这种情况下,您应该在list
单个成员上调用find_all()
,假设您需要div.hotel-wrap
以及相应的h3
一些信息:
for hotel in hotelInfo:
hotelTitle = hotel.find_all('h3', attrs={'class': 'p-name'})
If you only need the h3
elements, you can combine the two selectors to get them directly without having to find hotelInfo
first : 如果只需要h3
元素,则可以组合两个选择器直接获取它们,而不hotelInfo
查找hotelInfo
:
hotelTitle = soup.select('div.hotel-wrap h3.p-name')
For hotelinfo ,hoteltitle in zip (hotelinfos,hoteltitles): Data={ 'hotelinfo':hotelinfo.get_text(), } Print(data) 对于hotelinfo,邮政编码中的酒店标题(hotelinfos,酒店标题):Data = {'hotelinfo':hotelinfo.get_text(),}打印(数据)
Like that 像那样
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.