为什么更新此查询时页面响应没有更改?
Why doesn't the page response change when this query is updated?
我无法在网站 https://www.booking.com.
的页面中可靠地提取变量(属性 计数)
搜索巴西时,显示 29,454 个属性。
但是当尝试将查询更新为针对不同的国家/地区时,它会列出相同的数字(正负 1)。我不确定这是否与 headers 或查询有关。
也许有更简单的方法来提取信息
巴西应该有 29,000 多处房产,乌拉圭应该有 1,629 处
下面的代码应该像在 Booking.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
进行硬编码。 dest_id
30 简直指向巴西!
您可以使用以下方式验证:
querystring = querystring = {"src": country,
"dest_id": "225", "dest_type": "country",
}
请注意,为了简化,我删除了很多东西,但我最重要的是将 dest_id
更改为 225。225 是乌拉圭的 dest_id
,而 dest_id
30 (您硬编码的那个)是巴西。
每次你做你的请求,你都在请求巴西的信息,所以你得到的是相同的号码!将此 querystring
插入,您应该会看到乌拉圭的信息。
我不确定自动填充它的最佳方法是什么,也许只是查找您感兴趣的代码并将它们保存在字典中?这样每次循环你都会得到正确的 dest_id.
事实上,您将 country
插入到 (ssne, src, ssne_untouched) 中的 querystring
中的其他字符串中的 none 甚至有助于最终结果。您可以使用我示例中的 3 个字段提取乌拉圭信息。
我无法在网站 https://www.booking.com.
的页面中可靠地提取变量(属性 计数)搜索巴西时,显示 29,454 个属性。
但是当尝试将查询更新为针对不同的国家/地区时,它会列出相同的数字(正负 1)。我不确定这是否与 headers 或查询有关。
也许有更简单的方法来提取信息
巴西应该有 29,000 多处房产,乌拉圭应该有 1,629 处
下面的代码应该像在 Booking.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
进行硬编码。 dest_id
30 简直指向巴西!
您可以使用以下方式验证:
querystring = querystring = {"src": country,
"dest_id": "225", "dest_type": "country",
}
请注意,为了简化,我删除了很多东西,但我最重要的是将 dest_id
更改为 225。225 是乌拉圭的 dest_id
,而 dest_id
30 (您硬编码的那个)是巴西。
每次你做你的请求,你都在请求巴西的信息,所以你得到的是相同的号码!将此 querystring
插入,您应该会看到乌拉圭的信息。
我不确定自动填充它的最佳方法是什么,也许只是查找您感兴趣的代码并将它们保存在字典中?这样每次循环你都会得到正确的 dest_id.
事实上,您将 country
插入到 (ssne, src, ssne_untouched) 中的 querystring
中的其他字符串中的 none 甚至有助于最终结果。您可以使用我示例中的 3 个字段提取乌拉圭信息。