python 请求获取状态码 200 但 response.content 为空
python request get status code 200 but response.content is empty
我试图通过python请求获取this网站信息,点击绘图按钮(红色按钮)后,浏览器将收到3个响应,例如:
- [获取]https://invest.wessiorfinance.com/searchid.html?Stock=2330
- [获取]https://invest.wessiorfinance.com/Stock_api/Notation_cal?Stock=2330&Odate=2022-04-11&Period=3.5&is_log=0&is_adjclose=0
- [获取]https://invest.wessiorfinance.com/stock_api/Big_Trend?Stock=2330&Odate=2022-04-11&Period=3.5
而且我发现第一个响应将通过 {'ci_session': 'random string'} 设置 cookie,
然后使用带有 cookie 的会话来获得第二和第三个响应。
这是我的代码:
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')))
通过这种方式,我可以获得成功响应,然后我检查了 session.cookies.get_dict()
它表明有一个新的 cookie {'ci_session': 'random string'}。
所以我使用同一个会话来获得第二个 url 的响应,这是我的代码:
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)
我很确定这个会话的 cookie 已经有了新项目“ci_session”,
status_code 始终为 200 但 response.content 为空 (b'')!
这让我很困惑!有什么建议吗?
谢谢!
我从浏览器复制了请求,然后我得到了 api 到 return 数据:
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)
我试图通过python请求获取this网站信息,点击绘图按钮(红色按钮)后,浏览器将收到3个响应,例如:
- [获取]https://invest.wessiorfinance.com/searchid.html?Stock=2330
- [获取]https://invest.wessiorfinance.com/Stock_api/Notation_cal?Stock=2330&Odate=2022-04-11&Period=3.5&is_log=0&is_adjclose=0
- [获取]https://invest.wessiorfinance.com/stock_api/Big_Trend?Stock=2330&Odate=2022-04-11&Period=3.5
而且我发现第一个响应将通过 {'ci_session': 'random string'} 设置 cookie, 然后使用带有 cookie 的会话来获得第二和第三个响应。
这是我的代码:
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')))
通过这种方式,我可以获得成功响应,然后我检查了 session.cookies.get_dict() 它表明有一个新的 cookie {'ci_session': 'random string'}。 所以我使用同一个会话来获得第二个 url 的响应,这是我的代码:
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)
我很确定这个会话的 cookie 已经有了新项目“ci_session”, status_code 始终为 200 但 response.content 为空 (b'')! 这让我很困惑!有什么建议吗? 谢谢!
我从浏览器复制了请求,然后我得到了 api 到 return 数据:
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)