import json def get_headers(input_headers_string): # 使用三引号将浏览器复制出来的requests headers参数赋值给一个变量 headers = str(input_headers_string) # 去除参数头尾的空格并按换行符分割 headers = headers.strip().split('\n') # 使用字典生成式将参数切片重组,并去掉空格,处理带协议头中的:// headers = {x.split(':')[0].strip(): ("".join(x.split(':')[1:])).strip().replace('//', "://") for x in headers} # 使用json模块将字典转化成json格式打印出来 return_headers = json.dumps(headers, indent=1) print('headers={}'.format(return_headers)) return if __name__ == '__main__': headers = """ GET /preorder/api/v2.1/restaurants/list?tabUniqueId=1f7bb0e8-d5fc-408a-8cec-8dea306edf14&targetTime=2022-05-31+16:00&client_id=Xqr8w0Uk4ciodqfPwjhav5rdxTaYepD&client_secret=vD11O6xI9bG3kqYRu9OyPAHkRGxLh4E HTTP/1.1 Host: www.meican.com User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:99.0) Gecko/20100101 Firefox/99.0 Accept: application/json, text/plain, */* Accept-Language: zh Accept-Encoding: gzip, deflate, br Cache-Control: no-cache Pragma: no-cache Expires: 0 Connection: keep-alive Referer: https://www.meican.com/?date=1653926400&key=1f7bb0e8-d5fc-408a-8cec-8dea306edf14X1653984000000 Cookie: cna=6a4f8fc205104bf2b6928ca8d4dc72a5; machineId=4bdad021-23fc-4d48-a0c9-a7f94a699ea0; guestId=90ae2033-f68e-4afa-9470-994185b2efc1; remember=daa841ebe836f4cae6810f9bb42344ca08e00734-2012851; PLAY_FLASH="inner_authorized=true" Sec-Fetch-Dest: empty Sec-Fetch-Mode: cors Sec-Fetch-Site: same-origin """ get_headers(headers)
可以快速格式化,挪到Python代码中用
版权声明:如无特殊说明,文章均为本站原创,转载请注明出处
本文链接:http://kkxl95.cn/article/1653898382286/