首页 > 解决方案 > 我尝试了很多次从 booking.com 获取数据。但我做不到

问题描述

我想从 booking.com抓取数据,但出现了一些错误,找不到任何类似的代码。我想酒店的名称,价格等。

我尝试了 beautifulSoup 4 并尝试将数据保存到 csv 文件中。

import requests
from bs4 import BeautifulSoup
import pandas

# Replace search_url with a valid one byb visiting and searching booking.com
search_url = 'https://www.booking.com/searchresults.....'
page = requests.get(search_url)
soup = BeautifulSoup(page.content, 'html.parser')

week = soup.find(id = 'search_results_table'  )
#print(week)

items = week.find_all(class_='sr-hotel__name')
print(items[0])
print(items[0].find(class_ = 'sr-hotel__name').get_text())
print(items[0].find(class_ = 'short-desc').get_text())

是一个示例 URL,可以用来代替search_url.

这是错误消息...

<span class="sr-hotel__name " data-et-click="
">
The Fort Printers
</span>
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-44-77b38c8546bb> in <module>
     11 items = week.find_all(class_='sr-hotel__name')
     12 print(items[0])
---> 13 print(items[0].find(class_ = 'sr-hotel__name').get_text())
     14 print(items[0].find(class_ = 'short-desc').get_text())
     15 

AttributeError: 'NoneType' object has no attribute 'get_text'

标签: pythonweb-scrapingbeautifulsoupdata-science

解决方案


首先,伙计,使用请求可能真的很难,因为您必须完全模仿浏览器将发送的请求。您将不得不使用一些嗅探工具(burp、fiddler、wireshark),或者在某些情况下,在浏览器上以开发人员模式查看网络,这相对困难......

我建议您使用“selenium”,它是一种网络驱动程序,可以让您在尝试抓取网站时变得轻松......在这里阅读更多相关信息 - https://medium.com/the-andela-way/introduction- to-web-scraping-using-selenium-7ec377a8cf72

至于你的错误,我认为你应该只使用 .text 而不是 .get_text()


推荐阅读