python - 我尝试了很多次从 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'
解决方案
首先,伙计,使用请求可能真的很难,因为您必须完全模仿浏览器将发送的请求。您将不得不使用一些嗅探工具(burp、fiddler、wireshark),或者在某些情况下,在浏览器上以开发人员模式查看网络,这相对困难......
我建议您使用“selenium”,它是一种网络驱动程序,可以让您在尝试抓取网站时变得轻松......在这里阅读更多相关信息 - https://medium.com/the-andela-way/introduction- to-web-scraping-using-selenium-7ec377a8cf72
至于你的错误,我认为你应该只使用 .text 而不是 .get_text()
推荐阅读
- html - 简单的 5 星,但希望响应迅速
- java - 重新启动 appender 时,Chronicle Queue tailer 停止
- heroku - 在heroku上部署具有gdal依赖项的dash应用程序失败
- css - 第一个孩子的目标伪元素
- ruby-on-rails - `in_timezone` 接受哪些参数值?
- javascript - 从客户端向服务器端发送路由和数据时。我没有在服务器端接收数据
- macos - 在 Mac OSX 终端上合并 .csv 文件之间不使用新行
- android - 未找到支持 v7 的 SortedList
- python - 生存分析中如何处理不可逆矩阵
- spring - RabbitHealthIndicator - 健康检查失败