我在爲網站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
啊,那就做吧。我花了一些時間來了解發生了什麼。起初很混亂! –