我想抓取一个网页内的api的内容。 当网页请求接口时,我从Chrome 的开发者工具里边复制出了对应的js fetch代码,我把代码发到NodeJS里边可以正常运行,且可以捕获api返回的内容。 但是我把 Chrome里边的 cURl bash 内容复制到postman 请求时,却出现了防火墙的提示。
如下图:
我的js代码如下:
fetch("https://api.example.com/api", {
headers: {
accept: "application/json, text/plain, */*",
"accept-language": "zh-CN,zh;q=0.9",
"cache-control": "no-cache",
"content-type": "application/json",
"firebase-auth": "true",
"firebase-token": "<soem token>",
pragma: "no-cache",
"sec-ch-ua":
'"Google Chrome";v="107", "Chromium";v="107", "Not=A?Brand";v="24"',
"sec-ch-ua-mobile": "?0",
"sec-ch-ua-platform": '"Windows"',
"sec-fetch-dest": "empty",
"sec-fetch-mode": "cors",
"sec-fetch-site": "same-site",
},
referrer: "https://app.example.com/",
referrerPolicy: "strict-origin-when-cross-origin",
body: '{"action":"test","draftId":"","start":0,"end":17,"text":"hey what\'s up man","isBatch":false,"lookaheadIndex":0,"selection":{"bulletText":"","start":0,"end":17,"wholeText":"hey what\'s up man"},"languageCode":"en"}',
method: "POST",
mode: "cors",
credentials: "omit",
})
.then((res) => res.json())
.then((d) => console.log(d));
我把js代码的内容改写成python request的形式。但是运行时也得到跟postman一样的错误。
import requests
import json
url = "https://api.example.com/api"
payload = json.dumps({
"action": "REWRITE",
"draftId": "",
"start": 0,
"end": 8,
"text": "hey,man.",
"isBatch": False,
"lookaheadIndex": 0,
"selection": {
"bulletText": "",
"start": 0,
"end": 8,
"wholeText": "hey,man."
},
"languageCode": "en"
})
headers = {
'authority': 'https://api.example.com/api',
'accept': 'application/json, text/plain, */*',
'accept-language': 'zh-CN,zh;q=0.9',
'cache-control': 'no-cache',
'content-type': 'application/json',
'firebase-auth': 'true',
'firebase-token': '<soem token>',
'origin': 'https://app.example.com/',
'pragma': 'no-cache',
'referer': 'https://app.example.com/',
'sec-ch-ua': '"Google Chrome";v="107", "Chromium";v="107", "Not=A?Brand";v="24"',
'sec-ch-ua-mobile': '?0',
'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/107.0.0.0 Safari/537.36'
}
response = requests.request("POST", url, headers=headers, data=payload)
print(response.text)
请教大家,我应该怎么修改才能正常获得api返回的内容呢? 求指点,非常感谢。