2017-04-13 60 views
1

我在为网站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 

回答

1

你的问题是你硬编码dest_id。 30个简单的指向巴西的dest_id

您可以通过以下验证:

querystring = querystring = {"src": country, 
       "dest_id": "225", "dest_type": "country", 
       } 

注意,我删除了很多东西简化,但我最重要的改变dest_id到225 225 Uraguay的dest_id,而dest_id 30(你硬编码的那个)是巴西。

每当你提出要求时,你都在要求巴西的信息,所以你得到了相同的号码!插入这个querystring,你应该看到乌拉圭的信息。

我不确定最好的方法是自动填充它,也许只是查找你感兴趣的代码并将它们保存在字典中?这种方式每次通过循环你得到正确的dest_id。

事实上,您插入country到(ssne,src,ssne_untouched)的querystring中的其他字符串都不会影响最终结果。您可以使用我的示例中的3个字段来提取Uraguays信息。

+0

感谢您的回复。我记得有一个盲点,因为我记得查看一个显然没有将dest_id作为变量的国家的另一个查询。 – Phillip

+1

是的,为了确认,我一直在关注的原始查询是在首页(没有dest_id)的国家搜索查询 – Phillip

+1

啊,那就做吧。我花了一些时间来了解发生了什么。起初很混乱! –

相关问题