我在雅虎筛选器上运行了查询:
https://finance.yahoo.com/screener/equity/new
DevTools 显示数据通过以下方式以 JSON 形式返回:
所以我尝试使用以下方式手动请求数据:
import json
import requests
url = "https://query2.finance.yahoo.com/v1/finance/screener"
payload = json.loads('{"size":25,"offset":0,"sortField":"intradaymarketcap","sortType":"DESC","quoteType":"EQUITY","topOperator":"AND","query":{"operator":"AND","operands":[{"operator":"or","operands":[{"operator":"EQ","operands":["region","us"]}]},{"operator":"or","operands":[{"operator":"LT","operands":["intradaymarketcap",2000000000]},{"operator":"BTWN","operands":["intradaymarketcap",2000000000,10000000000]}]}]},"userId":"","userIdType":"guid"}')
header = {
"authority": "query2.finance.yahoo.com",
"method":"POST",
"path":"/v1/finance/screener?crumb=umZV3T8[ETC...]&lang=en-US®ion=US&formatted=true&corsDomain=finance.yahoo.com",
"scheme":"https",
"Accept":"*/*",
"Accept-Encoding":"gzip, deflate, br",
"Accept-Language":"en-US,en;q=0.9",
"Access-Control-Request-Headers":"content-type",
"Access-Control-Request-Method":"POST",
"Cache-Control":"no-cache",
"Content-Type":"application/json",
"Cookie":"tbla_id=33c52a3f-2fd9-41[ETC...]",
"Origin":"https://finance.yahoo.com",
"Pragma":"no-cache",
"Referer":"https://finance.yahoo.com/screener/equity/new",
"Sec-Ch-Ua":"\"Chromium\";v=\"116\",\"Google Chrome\";v=\"116\"",
"Sec-Ch-Ua-Platform":"Windows",
"Sec-Fetch-Dest":"empty",
"Sec-Fetch-Mode":"cors",
"Sec-Fetch-Site":"same-site",
"User-Agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/116.0.0.0 Safari/537.36"
}
response = requests.post(
url = url,
headers = header,
data = json.dumps(payload),
timeout = 30)
data_json = json.loads(response.content)
即使我使用原始请求标头中的 cookie 和 crumb,我也会收到此错误:
{'code': 'Unauthorized', 'description': 'Invalid Crumb'}
这是否可以通过请求实现?
看来,你需要设置额外的cookie
A1
才能得到正确的答案。您应该在 Web 开发人员工具中找到 cookie 的值:
import requests
api_url = "https://query1.finance.yahoo.com/v1/finance/screener"
payload = {
"offset": 0,
"query": {
"operands": [
{
"operands": [{"operands": ["region", "us"], "operator": "EQ"}],
"operator": "or",
}
],
"operator": "AND",
},
"quoteType": "EQUITY",
"size": 25,
"sortField": "intradaymarketcap",
"sortType": "DESC",
"topOperator": "AND",
"userId": "",
"userIdType": "guid",
}
params = {
"crumb": "EwuCwsPbKM2",
"lang": "en-US",
"region": "US",
"formatted": "true",
"corsDomain": "finance.yahoo.com",
}
headers = {
"User-Agent": "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:109.0) Gecko/20100101 Firefox/117.0",
}
with requests.session() as s:
s.cookies[
"A1"
] = "d=AQABBK8KXmQCEA8-VE0dBLqG5QEpQ7OglmEFEgABCAFH_2QyZfNtb2UB9qMAAAcIqgpeZJj7vK8&S=AQAAAnAOty-NkkMJle5hzDjUjSQ"
data = s.post(api_url, params=params, json=payload, headers=headers).json()
print(data)
打印:
{
"finance": {
"result": [
{
"start": 0,
"count": 25,
"total": 14459,
"quotes": [
{
"symbol": "AAPL",
"twoHundredDayAverageChangePercent": {
"raw": 0.07432321,
"fmt": "7.43%",
},
"dividendDate": {
"raw": 1692230400,
"fmt": "2023-08-16",
"longFmt": "2023-08-16T20:00",
},
...