让接口数据秒变 python 代码

本贴最后更新于 1613 天前,其中的信息可能已经天翻地覆

简介

uncurl是一个库,允许您将curl请求转换为使用requests 的python代码。由于Chrome网络检查器具有的“copy as cURL”,因此该工具对于用python重新创建浏览器请求很有用。

安装

pip install uncurl

使用步骤

1. 抓取接口数据--Chrome浏览器

以课堂派获取学生作业接口为例,在浏览器获取相关接口,右键 “copy as cURL”
Chrome抓包

复制数据如下

curl 'https://www.ketangpai.com/HomeworkApi/listsHomework?courseid=MDAwMDAwMDAwMLOsx9uGz79r' -H 'sec-fetch-mode: cors' -H 'cookie: ketangpai_home_favicon=%2Ffavicon.ico; PHPSESSID=cds5589atpq5lrtpgetvs1ho86; ketangpai_home_remember=think%3A%7B%22username%22%3A%22MDAwMDAwMDAwMLOcx92Gqc1shbiClLG6etmZtmfOk2RhbQ%22%2C%22expire%22%3A%22MDAwMDAwMDAwMLOGuZaIubOxhqiGmLOmdZ4%22%2C%22token%22%3A%22MDAwMDAwMDAwMMurrpWavLehhs1-3rHPo5OFt4vbepuomcWmmqaMiHtnr5ypzYWosKKZq6HQxtOK0ZCme5p-q6iZu2yrn4uNhJ3KedDYk7ivboS4ipazuYXahaeDmIB6YW0%22%7D; SERVERID=46de5db6e37d7dfd4998e2f5b47fad92|1569328495|1569328476' -H 'accept-encoding: gzip, deflate, br' -H 'accept-language: zh-CN,zh;q=0.9' -H 'user-agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/77.0.3865.90 Safari/537.36' -H 'accept: application/json, text/javascript, */*; q=0.01' -H 'referer: https://www.ketangpai.com/Course/homework/courseid/MDAwMDAwMDAwMLOsx9uGz79r.html' -H 'authority: www.ketangpai.com' -H 'x-requested-with: XMLHttpRequest' -H 'sec-fetch-site: same-origin' --compressed

2. 执行uncurl命令,转化数据包为python代码

回到命令行中执行

例子如下:

uncurl "curl 'https://www.ketangpai.com/HomeworkApi/listsHomework?courseid=MDAwMDAwMDAwMLOsx9uGz79r' -H 'sec-fetch-mode: cors' -H 'cookie: ketangpai_home_favicon=%2Ffavicon.ico; PHPSESSID=cds5589atpq5lrtpgetvs1ho86; ketangpai_home_remember=think%3A%7B%22username%22%3A%22MDAwMDAwMDAwMLOcx92Gqc1shbiClLG6etmZtmfOk2RhbQ%22%2C%22expire%22%3A%22MDAwMDAwMDAwMLOGuZaIubOxhqiGmLOmdZ4%22%2C%22token%22%3A%22MDAwMDAwMDAwMMurrpWavLehhs1-3rHPo5OFt4vbepuomcWmmqaMiHtnr5ypzYWosKKZq6HQxtOK0ZCme5p-q6iZu2yrn4uNhJ3KedDYk7ivboS4ipazuYXahaeDmIB6YW0%22%7D; SERVERID=46de5db6e37d7dfd4998e2f5b47fad92|1569328495|1569328476' -H 'accept-encoding: gzip, deflate, br' -H 'accept-language: zh-CN,zh;q=0.9' -H 'user-agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/77.0.3865.90 Safari/537.36' -H 'accept: application/json, text/javascript, */*; q=0.01' -H 'referer: https://www.ketangpai.com/Course/homework/courseid/MDAwMDAwMDAwMLOsx9uGz79r.html' -H 'authority: www.ketangpai.com' -H 'x-requested-with: XMLHttpRequest' -H 'sec-fetch-site: same-origin' --compressed"

win

即可将数据包转换为requests请求的python代码了

pbpaste | uncurl

macos

今天的小技能你get到了吗?
5 回帖
请输入回帖内容 ...