使用python将请求的requests headers参数格式化方法
作者:阿力多多 时间:2021-06-12 07:22:49
如下所示:
import json
# 使用三引号将浏览器复制出来的requests headers参数赋值给一个变量
headers = """
Host: zhan.qq.com
Proxy-Connection: keep-alive
Content-Length: 799432
Pragma: no-cache
Cache-Control: no-cache
Origin: http://zhan.qq.com
User-Agent: Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.181 Safari/537.36
Content-Type: multipart/form-data; boundary=----WebKitFormBoundaryuptjZg9xmsfWzngP
Accept: */*
Referer: http://zhan.qq.com/edit/site/beta/2005406545/index.html?164097
Accept-Encoding: gzip, deflate
Accept-Language: zh-CN,zh;q=0.9,en;q=0.8
Cookie: pgv_pvi=4342937600; RK=xFr4lrf07R; ptcz=a247c6170fb56cb4d5d41bf8461f11097a9be27bc7e7dc8b921254de9b1c924b;
"""
# 去除参数头尾的空格并按换行符分割
headers = headers.strip().split('\n')
# 使用字典生成式将参数切片重组,并去掉空格,处理带协议头中的://
headers = {x.split(':')[0].strip(): ("".join(x.split(':')[1:])).strip().replace('//', "://") for x in headers}
# 使用json模块将字典转化成json格式打印出来
print(json.dumps(headers,indent=1))
来源:https://blog.csdn.net/weixin_41164823/article/details/80524100
标签:python,requests,headers
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
玩转python爬虫之爬取糗事百科段子
2022-09-23 12:20:52
![](https://img.aspxhome.com/file/2023/2/69622_0s.jpg)
python处理emoji表情(两个函数解决两者之间的联系)
2022-12-03 18:51:12
![](https://img.aspxhome.com/file/2023/3/101133_0s.jpg)
写了一个小巧的jquery拾色工具
2009-12-21 14:22:00
![](https://img.aspxhome.com/file/UploadPic/20101/11/20091110144913-23s.gif)
详解如何用Golang处理每分钟100万个请求
2023-10-12 20:30:30
![](https://img.aspxhome.com/file/2023/8/103038_0s.webp)
在python中list作函数形参,防止被实参修改的实现方法
2022-11-15 19:27:25
![](https://img.aspxhome.com/file/2023/7/87777_0s.jpg)
基于Python爬取京东双十一商品价格曲线
2022-10-22 04:04:42
![](https://img.aspxhome.com/file/2023/7/89097_0s.jpg)
9个个人网站制作诀窍
2007-12-13 11:36:00
Python上传package到Pypi(代码简单)
2022-04-21 17:09:33
Tensorflow实现卷积神经网络的详细代码
2022-02-20 22:14:06
支持多风格变换的ASP分页类
2007-10-13 18:48:00
Python中一般处理中文的几种方法
2023-10-11 01:45:23
Django实现将views.py中的数据传递到前端html页面,并展示
2022-04-04 10:44:56
![](https://img.aspxhome.com/file/2023/2/72262_0s.jpg)
python 列表套json字典根据相同的key筛选数据
2022-06-01 08:52:02
asp如何验证IP地址?
2010-06-10 18:37:00
python项目报错:bs4.FeatureNotFound: Couldn‘t find a tree builder with the features you requests
2022-07-17 05:39:02
![](https://img.aspxhome.com/file/2023/4/92114_0s.jpg)
ASP中使用FileSystemObject时提高性能的一个小技巧
2008-06-08 13:09:00
WEB2.0网页制作标准教程(8)CSS布局入门
2007-09-11 13:21:00
![](https://img.aspxhome.com/file/uploadpic/20079/11/200791113246711.gif)
Python集中化管理平台Ansible介绍与YAML简介
2023-09-23 12:52:34
![](https://img.aspxhome.com/file/2023/6/88026_0s.png)
MySql常用命令总结
2009-09-16 10:51:00
SQL Server 2005返回刚刚插入的数据条目id值
2008-12-04 17:16:00