如何在http请求python中获取所有结果

时间:2017-01-04 08:50:26

标签: python json httprequest python-requests python-3.5

我想从https://www.ncl.com/获得所有结果。我发现该请求必须为GET并发送到此链接:https://www.ncl.com/search_vacations 到目前为止,我得到了前12个结果,解析它们没有问题。问题是我找不到“改变”结果页面的方法。我得到499中的12,我需要得到它们。我试图这样做https://www.ncl.com/search_vacations?current_page=1并且每次都增加它但我每次都得到相同(第一)的结果。尝试再次将json正文添加到请求json = {"current_page": '1'},但没有成功。 到目前为止,这是我的代码:

    import math
import requests

session = requests.session()
proxies = {'https': 'https://97.77.104.22:3128'}
headers = {
    "authority": "www.ncl.com",
    "method": "GET",
    "path": "/search_vacations",
    "scheme": "https",
    "accept": "application/json, text/plain, */*",
    "connection": "keep-alive",
    "referer": "https://www.ncl.com",
    "cookie": "AkaUTrackingID=5D33489F106C004C18DFF0A6C79B44FD; AkaSTrackingID=F942E1903C8B5868628CF829225B6C0F; UrCapture=1d20f804-718a-e8ee-b1d8-d4f01150843f; BIGipServerpreprod2_www2.ncl.com_http=61515968.20480.0000; _gat_tealium_0=1; BIGipServerpreprod2_www.ncl.com_r4=1957341376.10275.0000; MP_COUNTRY=us; MP_LANG=en; mp__utma=35125182.281213660.1481488771.1481488771.1481488771.1; mp__utmc=35125182; mp__utmz=35125182.1481488771.1.1.utmccn=(direct)|utmcsr=(direct)|utmcmd=(none); utag_main=_st:1481490575797$ses_id:1481489633989%3Bexp-session; s_pers=%20s_fid%3D37513E254394AD66-1292924EC7FC34CB%7C1544560775848%3B%20s_nr%3D1481488775855-New%7C1484080775855%3B; s_sess=%20s_cc%3Dtrue%3B%20c%3DundefinedDirect%2520LoadDirect%2520Load%3B%20s_sq%3D%3B; _ga=GA1.2.969979116.1481488770; mp__utmb=35125182; NCL_LOCALE=en-US; SESS93afff5e686ba2a15ce72484c3a65b42=5ecffd6d110c231744267ee50e4eeb79; ak_location=US,NY,NEWYORK,501; Ncl_region=NY; optimizelyEndUserId=oeu1481488768465r0.23231006365903206",
    "Proxy-Authorization": "Basic QFRLLTVmZjIwN2YzLTlmOGUtNDk0MS05MjY2LTkxMjdiMTZlZTI5ZDpAVEstNWZmMjA3ZjMtOWY4ZS00OTQxLTkyNjYtOTEyN2IxNmVlMjlk"
}


def get_count():
    response = requests.get(
        "https://www.ncl.com/search_vacations?cruise=1&cruiseTour=0&cruiseHotel=0&cruiseHotelAir=0&flyCruise=0&numberOfGuests=4294953449&state=undefined&pageSize=10&currentPage=",
        proxies=proxies)
    tmpcruise_results = response.json()
    tmpline = tmpcruise_results['meta']
    total_record_count = tmpline['aggregate_record_count']
    return total_record_count


total_cruise_count = get_count()
total_page_count = math.ceil(int(total_cruise_count) / 10)
session.headers.update(headers)
cruises = []
page_counter = 1
while page_counter <= total_page_count:
    url = "https://www.ncl.com/search_vacations?current_page=" + str(page_counter) + ""
    page = requests.get(url, headers=headers, proxies=proxies)
    cruise_results = page.json()
    for line in cruise_results['results']:
        cruises.append(line)
        print(line)
    page_counter += 1
    print(cruise_results['pagination']["current_page"])
    print("----------")
print(len(cruises))

使用requests和代理。任何想法如何做到这一点?

1 个答案:

答案 0 :(得分:2)

该网站声称拥有12264个搜索结果(空白搜索),以12页为单位。

搜索网址采用参数Nao,似乎定义了结果页面开始的搜索结果偏移量。

提取https://www.ncl.com/uk/en/search_vacations?Nao=45

应该得到一个&#34;页面&#34; 12个搜索结果,从结果46开始。

果然:

"pagination": {

    "starting_record": "46",
    "ending_record": "57",
    "current_page": "4",
    "start_page": "1",
    ...

因此,要翻阅所有结果,请从Nao = 0开始,并为每次提取添加12。