tylrr 2022-11-06 17:15 采纳率: 84.6%
浏览 25
已结题

请问怎样把这段JS 的request请求转成python request的请求?

我想抓取一个网页内的api的内容。 当网页请求接口时,我从Chrome 的开发者工具里边复制出了对应的js fetch代码,我把代码发到NodeJS里边可以正常运行,且可以捕获api返回的内容。 但是我把 Chrome里边的 cURl bash 内容复制到postman 请求时,却出现了防火墙的提示。
如下图:

img

我的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返回的内容呢? 求指点,非常感谢。

  • 写回答

0条回答 默认 最新

    报告相同问题?

    问题事件

    • 系统已结题 11月14日
    • 修改了问题 11月6日
    • 创建了问题 11月6日

    悬赏问题

    • ¥15 有了解d3和topogram.js库的吗?有偿请教
    • ¥100 任意维数的K均值聚类
    • ¥15 stamps做sbas-insar,时序沉降图怎么画
    • ¥15 unity第一人称射击小游戏,有demo,在原脚本的基础上进行修改以达到要求
    • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
    • ¥15 关于#Java#的问题,如何解决?
    • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
    • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
    • ¥15 cmd cl 0x000007b
    • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line