2017-04-13 8 views
1

https://www.booking.com 웹 사이트의 페이지에서 변수 (속성 개수)를 안정적으로 추출하는 데 문제가 있습니다.이 쿼리가 업데이트 될 때 페이지 응답이 변경되지 않는 이유는 무엇입니까?

브라질을 검색 할 때 29,454 개의 속성을 표시합니다.

그러나 다른 국가의 쿼리를 업데이트하려고하면 동일한 숫자 (더하기 또는 빼기 1)가 나열됩니다. 헤더 또는 쿼리와 관련이 있는지 확실하지 않습니다.

아마

브라질 29,000+ 속성을 가지고 있어야 우루과이는 1629

가 다음 코드는 예약에서 국가의 이름을 검색하는 것처럼 작동 할 것으로 예상된다해야한다 정보를 추출하는 쉬운 방법이있다. co.kr

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을 하드 코드하고있는 중입니다. dest_id은 브라질을 가리킨다.

다음과 같은 사용하여 확인할 수 있습니다 : 나는 간단하게 할 수있는 물건을 많이 제거

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

참고,하지만 가장 중요한 (225) (225)에 dest_id 변경은 dest_id 동안, Uraguay의 dest_id입니다 30 (하드 코드 된 것)은 브라질이었습니다.

요청할 때마다 브라질의 정보를 요청 했으므로 같은 번호를 받았습니다. 이 querystring을 연결하면 Uruguay의 정보가 표시됩니다.

내가 가장 좋은 방법은 자동으로 채워지는지 모르겠다. 관심있는 코드를 검색하여 사전에 저장하는 것일까? 그럴 때마다 루프를 통해 올바른 dest_id를 얻을 수 있습니다.

실제로 country을 (ssne, src, ssne_untouched)에 연결 한 querystring의 다른 문자열 중 어느 것도 최종 결과에 포함되지 않습니다. 내 예제에서 3 개의 필드를 사용하여 Uraguays 정보를 가져올 수 있습니다.

+0

응답 해 주셔서 감사합니다. 분명히 dest_id를 변수로 가지지 않은 국가에 대한 다른 쿼리를 본 기억이 나는 사각 지대가있었습니다. – Phillip

+1

네, 확인하기 위해, myopically 고정되어 있던 원래 쿼리는 (dest_id가 없었던) 맨 앞 페이지의 국가 검색에 대한 쿼리였습니다. – Phillip

+1

아, 그래, 그럴거야. 무슨 일이 일어나고 있는지에 관해서는 나에게 조금 걸렸습니다. 처음에는 매우 혼란 스러웠습니다! –