首页 > 解决方案 > 用python实现http请求

问题描述

我想在 python 的帮助下模拟对 ok.ru 的请求

我做了什么

  1. 获取页面https://ok.ru/dk?st.cmd=searchResult&st.mode=Users&st.query=Ivan%20Black (完成)
  2. 输入文本以选择位置并出现弹出菜单
  3. 在浏览器开发工具中找到 与弹出菜单相对应的requstSuggestLocations (见图)

SuggestLocations是对https://ok.ru/web-api/v2/search/suggestLocation的请求,带有参数:

 // Request Headers
    :authority: ok.ru
    :method: POST
    :path: /web-api/v2/search/suggestLocations
    :scheme: https
    accept: application/json, text/javascript, */*; q=0.01
    accept-encoding: gzip, deflate, br
    accept-language: ru-RU,ru;q=0.9,en-US;q=0.8,en;q=0.7
    content-length: 89
    content-type: text/plain;charset=UTF-8
    cookie: bci=3082280438089561982; _statid=d33e9044-8d2b-4e5d-a3fd-e65ca657312f; _hd=h; tmr_lvid=2ac18e93052a4516697406103a6db5b3; tmr_lvidTS=1575669087459; _flashVersion=0; community-lang=ru; _userIds=""; klos=0; AUTHCODE=PbjA0slw51_fVLfNIPdAJdv_82O5bU04nyJmZ6bTK4mpqUJLojaxxW9TzFtp6W3-A6w04WD6bA3-gy2T5WoMnVtoZpGdqcbSTGAwmOGEza5tpQOJBbR6WH9MGuHIUu1723LUi0MLf-fzltk_3; JSESSIONID=8218b2b8c2812e5b4d36e84875004ebe2efbff9865492d2f.2eedc443; LASTSRV=ok.ru; msg_conf=2468555756792551; viewport=762; CDN=; TZ=6; nbp=; cudr=0; tmr_detect=1%7C1604937734327; tmr_reqNum=309; TZD=6.406; TD=406
    dnt: 1
    ok-screen: searchResult
    origin: https://ok.ru
    referer: https://ok.ru/
    sec-fetch-dest: empty
    sec-fetch-mode: cors
    sec-fetch-site: same-origin
    strd: false
    tkn: fryryeONJwI5hQOEHQc-05mvliDDbThF
    user-agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.75 Safari/537.36


 // Request payload
    '{"id": 39, "parameters": {"query": "Но","countryId": "10414533690"}}'

对此请求的响应是一组城市

{"id":"39","success":true,"result":[{"text":{"value":"Новосибирск","highlights":[{"from":0,"to":2}]}},{"text":{"value":"Новокузнецк","highlights":[{"from":0,"to":2}]}},{"text":{"value":"Новороссийск","highlights":[{"from":0,"to":2}]}},{"text":{"value":"Новочеркасск","highlights":[{"from":0,"to":2}]}},{"text":{"value":"Новомосковск","highlights":[{"from":0,"to":2}]}},{"text":{"value":"Новый Уренгой","highlights":[{"from":0,"to":2}]}},{"text":{"value":"Норильск","highlights":[{"from":0,"to":2}]}},{"text":{"value":"Ноябрьск","highlights":[{"from":0,"to":2}]}},{"text":{"value":"Ногинск","highlights":[{"from":0,"to":2}]}},{"text":{"value":"Новоалтайск","highlights":[{"from":0,"to":2}]}}]}

创建代码,但收到空输出。预期响应类似于上述响应

import json
import requests


def search_city():
    headers = {
        'authority': 'ok.ru',
        'method': 'POST',
        'path': '/web-api/v2/search/suggestLocations',
        'scheme': 'https',
        'accept': 'application/json, text/javascript, */*; q=0.01',
        'accept-encoding': 'gzip, deflate, br',
        'accept-language': 'ru-RU,ru;q=0.9,en-US;q=0.8,en;q=0.7',
        'content-length': '115',
        'content-type': 'text/plain;charset=UTF-8',
        'cookie': 'bci=3082280438089561982; _statid=d33e9044-8d2b-4e5d-a3fd-e65ca657312f; _hd=h; tmr_lvid=2ac18e93052a4516697406103a6db5b3; tmr_lvidTS=1575669087459; _flashVersion=0; community-lang=ru; _userIds=""; klos=0; AUTHCODE=PbjA0slw51_fVLfNIPdAJdv_82O5bU04nyJmZ6bTK4mpqUJLojaxxW9TzFtp6W3-A6w04WD6bA3-gy2T5WoMnVtoZpGdqcbSTGAwmOGEza5tpQOJBbR6WH9MGuHIUu1723LUi0MLf-fzltk_3; JSESSIONID=8218b2b8c2812e5b4d36e84875004ebe2efbff9865492d2f.2eedc443; LASTSRV=ok.ru; msg_conf=2468555756792551; viewport=762; CDN=; TZ=6; nbp=; cudr=0; tmr_detect=1%7C1604937734327; tmr_reqNum=309; TZD=6.406; TD=406',
        'dnt': '1',
        'ok-screen': 'searchResult',
        'origin': 'https://ok.ru',
        'referer': 'https://ok.ru/',
        'sec-fetch-dest': 'empty',
        'sec-fetch-mode': 'cors',
        'sec-fetch-site': 'same-origin',
        'strd': 'false',
        'tkn': 'fryryeONJwI5hQOEHQc-05mvliDDbThF',
        'user-agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.75 Safari/537.36'
    }
    
    """ Attempt 1
    data = {
      'data': '{"id": 39, "parameters": {"query": "Но","countryId": "10414533690"}}'
    }"""

    # Attempt 2
    data = {"id": 39, "parameters": {"query": "Но","countryId": "10414533690"}}

url = "https://www.facebook.com/api/graphql/"
    url = "https://www.facebook.com/api/graphql/"

    response = requests.post(url, headers=headers, data=data)
    print(response) # <Response [200]>
    html = response.text
    print(html) # empty

search_city()

问题:我怎样才能收到预期的 json 响应?

在此处输入图像描述

标签: pythonweb-scrapingrequest

解决方案


推荐阅读