Python-如何在beautifulsoup中获取内部类文本(TripAdvisor)

时间:2018-09-10 14:34:23

标签: python-3.x beautifulsoup

我正试图通过python web scrap程序在TripAdvisor的特定日期范围内获取特定区域内特定区域内所有酒店的价格。我的程序使用硒选择日期范围以及在将数据解析为BeautifulSoup时向网站加载。 价格数据位于站点的内部类中。 Inspect element

我正在使用此代码,并给我ResultSet对象没有属性错误。

html = browser.page_source
textobj = BeautifulSoup(html,"html.parser")
text1=textobj.find_all('div', attrs={'class': 'vr_listing'})
for item in text1:
     foo=item.find_all('div', attrs={'class' : 'price'})
     price=foo.text.strip()
     print(price)

使用Python 3.7 无法弄清楚该怎么办。

2 个答案:

答案 0 :(得分:0)

如果您可以提供所使用的链接,这样我们可以重现此问题,那就太好了。但是您可以尝试以下代码行吗:

html = browser.page_source
textobj = BeautifulSoup(html,"html.parser")
prices = textobj.findAll('div', {'class':'price'}).text

for price in prices:
    print(price)

'''
text1=textobj.find_all('div', attrs={'class': 'vr_listing'})
for item in text1:
     foo=item.find_all('div', attrs={'class' : 'price'})
     price=foo.text.strip()
     print(price)
'''

答案 1 :(得分:0)

这里:

 foo = item.find_all('div', attrs={'class' : 'price'})
 price = foo.text.strip()

find_all()返回resultSet(可在标记集合上迭代),而不是单个标记。如果您希望item中只有一个匹配标签,请改用item.find(...)

 foo = item.find('div', attrs={'class' : 'price'})
 price = foo.text.strip()

否则遍历结果集:

 foos = item.find_all('div', attrs={'class' : 'price'})
 prices = [foo.text.strip() for foo in foos]
相关问题