无法从Booking * com使用BeautifulSoup find()获得所需的标签

时间:2019-07-04 13:33:35

标签: python python-3.x beautifulsoup

我正在尝试使用BeautifulSoup从Booking * com抓取日期,但是find()什么也没有返回

我想获取在Booking * com搜索result page

上首先列出的一家旅馆的价格

HTTP响应填充良好,我成功地从页面中获取了一些标签,但无法检索到所需的标签。

我已经尝试了几种论证模式,但是结果是相同的;

find(class_="bui-price-display__value") 
find("div", class_="bui-price-display__value") 
find("div",{"class":"bui-price-display__value"})

这是整个代码;

import requests
from bs4 import BeautifulSoup

request = requests.get ("https://www.booking.com/searchresults.ja.html?checkin_year=2019&checkin_month=9&checkin_monthday=3&checkout_year=2019&checkout_month=9&checkout_monthday=4&no_rooms=1&group_adults=1&group_children=0&from_sf=1&ac_position=0&ac_langcode=ja&dest_id=434312&dest_type=hotel&search_selected=true&ac_suggestion_list_length=1&ac_suggestion_theme_list_length=0&selected_currency=JPY")
soup = BeautifulSoup(request.text)
print( soup.find(class_="bui-price-display__value") )

我希望div标签包含¥2,275,但是它返回None。

1 个答案:

答案 0 :(得分:2)

在请求页面时添加User-Agent

import requests
from bs4 import BeautifulSoup
headers = {'User-Agent':'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/47.0.2526.106 Safari/537.36'}
request = requests.get ("https://www.booking.com/searchresults.ja.html?checkin_year=2019&checkin_month=9&checkin_monthday=3&checkout_year=2019&checkout_month=9&checkout_monthday=4&no_rooms=1&group_adults=1&group_children=0&from_sf=1&ac_position=0&ac_langcode=ja&dest_id=434312&dest_type=hotel&search_selected=true&ac_suggestion_list_length=1&ac_suggestion_theme_list_length=0&selected_currency=JPY",headers=headers)
soup = BeautifulSoup(request.text,'html.parser')
print(soup.find(class_="bui-price-display__value").text)