tylrr 2022-11-06 20:05 采纳率: 84.6%
浏览 68
已结题

请问怎样把JS Fetch的网络请求改成用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返回的内容呢? 求指点,非常感谢。

  • 写回答

4条回答 默认 最新

  • CSDN专家-showbo 2022-11-06 20:49
    关注

    今天看请求头变成token了,改下面的可以

    import urllib
    from urllib import request, parse
    import json
    import http.client
    http.client._MAXHEADERS = 1000#fix http.client.HTTPException: got more than 100 headers error
    
    data={"action":"REWRITE","draftId":"","start":0,"end":7,"text":"hey man","isBatch":False,"lookaheadIndex":0,"selection":{"bulletText":"","start":0,"end":7,"wholeText":"hey man"},"languageCode":"en"}
    data=json.dumps(data)
    headers={
        'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/104.0.0.0 Safari/537.36',
        'Content-Type':'application/json',
        #'firebase-auth':'true',
        'token':'token值,注意看浏览器实际发送的请求头是token还是firebase-token,这个请求头会变化,以浏览器的为准'
    }
     
    url = 'https://api.wordtune.com/rewrite'
     
    data = bytes(data, encoding='utf8')
    try:
        req = request.Request(url=url, data=data, headers=headers, method='POST')
        response = request.urlopen(req)
        #print(response.status,response.reason)
        print(response.read().decode('utf-8'))
    except urllib.error.HTTPError as e:
        # 用异常捕获,http状态码非200时,可解析出响应体
        print(e.read().decode("UTF-8"))
    
    

    以下备用,题主自己切换测试看

    import urllib
    from urllib import request, parse
    import json
    import http.client
    http.client._MAXHEADERS = 1000#fix http.client.HTTPException: got more than 100 headers error
    
    data={"action":"REWRITE","draftId":"","start":0,"end":7,"text":"hey man","isBatch":False,"lookaheadIndex":0,"selection":{"bulletText":"","start":0,"end":7,"wholeText":"hey man"},"languageCode":"en"}
    data=json.dumps(data)
    headers={
        'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/104.0.0.0 Safari/537.36',
        'Content-Type':'application/json',
        'firebase-auth':'true',
        'firebase-token':'aaa'
    }
     
    url = 'https://api.wordtune.com/rewrite'
    
    data = bytes(data, encoding='utf8')
    try:
        req = request.Request(url=url, data=data, headers=headers, method='POST')
        response = request.urlopen(req)
        print(response.status,response.reason)
        print(response.read().decode('utf-8'))
    except urllib.error.HTTPError as e:
        # 用异常捕获,http状态码非200时,可解析出响应体
        print(e.read().decode("UTF-8"))
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论 编辑记录
查看更多回答(3条)

报告相同问题?

问题事件

  • 系统已结题 11月17日
  • 已采纳回答 11月9日
  • 创建了问题 11月6日

悬赏问题

  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改
  • ¥20 wireshark抓不到vlan
  • ¥20 关于#stm32#的问题:需要指导自动酸碱滴定仪的原理图程序代码及仿真
  • ¥20 设计一款异域新娘的视频相亲软件需要哪些技术支持