应用场景
需要对浏览器某些请求二次开发,完成批量操作等自动开发
使用说明
F12->网络 选中请求,右键复制为curl(bash),然后替换到下面代码curl变量,然后运行生成python代码
import requests
import json
import re
# curl为浏览器请求右键复制的curl(bash)版本,其他curl暂未测试
def curl_requests():
curl = """
curl 'http://xxx' \
-H 'Accept: application/json, text/plain, */*' \
-H 'Accept-Language: zh-CN,zh;q=0.9' \
-H 'User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/101.0.0.0 Safari/537.36' \
--data-raw '{"expr":null,"datasourceType":[],"warehouseType":[],"pagination":{"pageNum":1,"pageSize":10,"datasourceType":null,"warehouseType":null,"sort":"desc","sortField":"update_time"}}' \
--compressed \
--insecure """
method = "GET"
payload = None
rows = re.split("[ ]{3,}", curl.strip())
url = re.match("curl \'(.*)\'", rows[0]).groups(0)[0]
header = {}
for row in rows[1:]:
if row.startswith("-H"):
groups = re.match("-H [$]?'([\w-]*): (.*)'", row).groups()
key = groups[0]
value = groups[1]
header[key] = value
if row.startswith("--data-raw"):
payload = re.match("--data-raw '(.*)'", row).groups(0)[0]
if payload is not None:
method = "POST"
print(f"url:{url}\nmethod:{method}\nheaders:{header}\npayload:{payload}\n")
print("generate python requests")
print(f"url='{url}'")
print(f"headers={header}")
print(f"payload={json.dumps(payload)}")
print(f"response = requests.requests('{method}',url,payload,headers)")
注意事项
手动编写的curl未测试是否兼容,主要是格式要保持一致,代码是按照浏览器生成的curl格式解析的
标签:
python
本文转载自: https://blog.csdn.net/qq_18453581/article/details/128583126
版权归原作者 Zakza 所有, 如有侵权,请联系我们删除。
版权归原作者 Zakza 所有, 如有侵权,请联系我们删除。