我在为网站https://www.booking.com的页面中可靠地提取变量(属性数量)时遇到问题。为什么在更新此查询时页面响应不会改变?
当搜索巴西时,它显示29,454属性。
但是,当试图更新查询为一个不同的国家,它列出了相同的数字(加号或减号1)。我不确定这是否与标题或查询有关。
也许有提取信息
巴西应该有29,000+性质和乌拉圭应该有1629
下面的代码预计仿佛寻找全国在预订名操作更简单的方法。 com
import requests
from bs4 import BeautifulSoup
from requests.packages.urllib3.exceptions import InsecureRequestWarning
requests.packages.urllib3.disable_warnings(InsecureRequestWarning)
url = "https://www.booking.com/searchresults.en-gb.html"
countries = [u'Brazil', u'Uruguay']
for country in countries:
querystring = {"label": "gen173nr-1DCAEoggJCAlhYSDNiBW5vcmVmcgV1c19vcogBAZgBMbgBB8gBDdgBA-gBAfgBApICAXmoAgM",
"lang": "en-gb", "sid": "5f9b0b3af27a0a0b48017c6c387d8224", "track_lsso": "2", "sb": "1",
"src": country, "src_elem": "sb",
"ss": country.replace(' ', '+'), "ssne": country, "ssne_untouched": country, "dest_id": "30", "dest_type": "country",
"checkin_monthday": "", "checkin_month": "", "checkin_year": "", "checkout_monthday": "",
"checkout_month": "", "checkout_year": "", "room1": "A", "no_rooms": "1", "group_adults": "1",
"group_children": "0"}
headers = {
'upgrade-insecure-requests': "1",
'user-agent': "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/57.0.2987.133 Safari/537.36",
'accept': "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8",
'content-encoding': "br",
'accept-language': "en-US,en;q=0.8",
'content-type': "text/html;charset=UTF-8",
'cache-control': "no-cache",
'postman-token': "124b1e3b-c4de-9ab0-162f-003770797f9f"
}
response = BeautifulSoup(requests.request("GET", url, headers=headers, params=querystring, verify=False).content,
"html.parser")
totalPropCount = response.select('h1[class="sorth1"]')[0].text
print totalPropCount.split(': ')[1], ' for ', country
感谢您的回复。我记得有一个盲点,因为我记得查看一个显然没有将dest_id作为变量的国家的另一个查询。 – Phillip
是的,为了确认,我一直在关注的原始查询是在首页(没有dest_id)的国家搜索查询 – Phillip
啊,那就做吧。我花了一些时间来了解发生了什么。起初很混乱! –