Python自动化 让接口数据秒变 python 代码

Jack · 2019年09月24日 · 最后由 zyt_123456 回复于 2019年11月21日 · 1090 次阅读
本帖已被设为精华帖!

简介

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格式数据"

例子如下:

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代码了

  • macOS可直接执行

pbpaste | uncurl

macos

今天的小技能你get到了吗?
共收到 5 条回复
star 将本帖设为了精华贴 09月24日 21:12

优秀的Jack

谢谢分享,特别好

文章浅显易懂,手动点赞

可优大佬讲课棒棒哒!18:04:27

需要 登录 后方可回复, 如果你还没有账号请点击这里 注册