简体   繁体   中英

python request get status code 200 but response.content is empty

I am trying to get this website information by python requests, after clicking the drawing button (red button), the browser will receive 3 responses, for example:

  1. [GET] https://invest.wessiorfinance.com/searchid.html?Stock=2330
  2. [GET] https://invest.wessiorfinance.com/Stock_api/Notation_cal?Stock=2330&Odate=2022-04-11&Period=3.5&is_log=0&is_adjclose=0
  3. [GET] https://invest.wessiorfinance.com/stock_api/Big_Trend?Stock=2330&Odate=2022-04-11&Period=3.5

And I found that first response will set cookie by {'ci_session': 'random string'}, then used the session with cookie to get second and third responses.

Here is my code:

import requests
import json
headers = {'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/100.0.4896.75 Safari/537.36','x-requested-with': 'XMLHttpRequest',}
session = requests.session()
url ="https://invest.wessiorfinance.com/searchid.html?Stock=2330"
response = session.get(url, headers = headers)
    if response.status_code == 200:
        print(json.loads(response.content.decode('utf-8')))

By this way, I can get success response and then I checked session.cookies.get_dict() it showed that there is a new cookie {'ci_session': 'random string'}. So I use same session to get second url's response, here is my code:

url2 = "https://invest.wessiorfinance.com/Stock_api/Notation_cal?Stock=2330&Odate=2022-04-11&Period=3.5&is_log=0&is_adjclose=0"
response = session.get(url2, headers = headers)
if response.status_code == 200:
    print(response.content)

I am pretty sure that this session's cookie already has new item "ci_session", the status_code is always 200 but response.content is empty(b'')? It confused me a lot! Is there any suggest? Thanks!

I copied the request from the browser and then i got the api to return data:

import requests

cookies = {
    'ci_session': 'kndj0o4bbor8a6fh8kafqunk3b',
}

headers = {
    'authority': 'invest.wessiorfinance.com',
    'accept': 'text/plain, */*; q=0.01',
    'accept-language': 'en-GB,en-US;q=0.9,en;q=0.8,nb;q=0.7,no;q=0.6,sv;q=0.5',
    'cache-control': 'no-cache',
    # 'cookie': 'ci_session=kndj0o4bbor8a6fh8kafqunk3b',
    'pragma': 'no-cache',
    'referer': 'https://invest.wessiorfinance.com/notation.html',
    'sec-ch-ua': '" Not A;Brand";v="99", "Chromium";v="100", "Google Chrome";v="100"',
    'sec-ch-ua-mobile': '?0',
    'sec-ch-ua-platform': '"Windows"',
    'sec-fetch-dest': 'empty',
    'sec-fetch-mode': 'cors',
    'sec-fetch-site': 'same-origin',
    'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/100.0.4896.75 Safari/537.36',
    'x-requested-with': 'XMLHttpRequest',
}

params = {
    'Stock': '2330',
    'Odate': '2022-04-11',
    'Period': '3.5',
}

response = requests.get('https://invest.wessiorfinance.com/stock_api/Big_Trend', headers=headers, params=params, cookies=cookies)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM