Python 为什么';更新此查询时页面响应是否会更改?

Python 为什么';更新此查询时页面响应是否会更改?,python,header,beautifulsoup,python-requests,response,Python,Header,Beautifulsoup,Python Requests,Response,我无法可靠地提取网站页面中的变量(属性计数) 搜索巴西时,显示29454个属性 但是,当试图将查询更新为其他国家时,它会列出相同的数字(正负1)。我不确定这是否与标题或查询有关 也许有更简单的方法来提取信息 巴西应该有29000多处房产,乌拉圭应该有1629处 以下代码的操作就像在Booking.com上搜索国家名称一样 import requests from bs4 import BeautifulSoup from requests.packages.urllib3.exceptions

我无法可靠地提取网站页面中的变量(属性计数)

搜索巴西时,显示29454个属性

但是,当试图将查询更新为其他国家时,它会列出相同的数字(正负1)。我不确定这是否与标题或查询有关

也许有更简单的方法来提取信息

巴西应该有29000多处房产,乌拉圭应该有1629处

以下代码的操作就像在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\u id
。30个目标id的
dest_id
只指向巴西

您可以使用以下方法进行验证:

querystring = querystring = {"src": country,
               "dest_id": "225", "dest_type": "country",
               }
注意,为了简化,我删除了很多东西,但最重要的是,我将
dest_id
更改为225。225是乌拉瓜的
dest_id
,而
dest_id
30(您硬编码的那一个)是巴西

每次你提出请求时,你都在请求巴西的信息,所以你得到了相同的号码!插入此
querystring
,您将看到乌拉圭的信息

我不确定自动填充的最佳方式是什么,也许只是查找您感兴趣的代码并将其保存在dict中?这样,每次通过循环都可以获得正确的dest_id


事实上,
querystring
中插入的
country
中的其他字符串(ssne、src、ssne_未触及)甚至都不计入最终结果。您可以使用我的示例中的3个字段调出乌拉圭信息。

感谢您的回复。我有一个盲点,因为我记得我查看了另一个显然没有dest_id作为变量的国家的查询。是的,为了确认,我一直目光短浅地关注的原始查询是首页上的国家搜索查询(没有dest_id)啊,是的,可以了。我花了一点时间才弄清楚到底发生了什么事。一开始很混乱!当然可以