欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

转换请求头参数的python脚本,自动添加引号

程序员文章站 2022-04-17 21:27:48
...

在爬虫的时候好多朋友都在纠结怎么快速请求头加引号和逗号隔开,每次都要一个一个加引号,很费时间,在这里给大家分享一个自己写的脚本,把请求头复制进去运行,再复制出来即可用,代码如下

headers= """
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8
Accept-Encoding: gzip, deflate, br
Accept-Language: zh-CN,zh;q=0.9
Cache-Control: max-age=0
Connection: keep-alive
Cookie: __jsluid=e0f50b42c0133479609728104ecfa0bdb; _ga=GA1.2.733796313.1544613362; _gid=GA1.2.1036781813.15446313362; Hm_lvt_c4e8e5b919a5c12647962ea08462e63b=1544613362; gr_user_id=29e5f963-4dbb-411a-9233-fb3caf3c7692; is_read_index_notice_=1; __guid=XynHr45c10edf29685b6.52148503; FISKCDDCC=b92099aa55b9f8ab4f431843e799e41d; _nyjy_newadv_=1; SHADOWMAN=%7B%22key%22%3A%2250442c70686cf47a4a6e3196e074f17df%22%2C%22val%22%3A%22e058341874ca0adecba52db27besf94f0%22%2C%22tm%22%3A1544614351%7D; channel_id=16; location_city_id=5; NY_VALIDATE_KEY=c20a31c6de4eaf9ad8b1a0d9670a525a; gr_session_id_88c697c1877e5045=ea12172c-4b96-4fdc-8398-3e8830a50124; gr_session_id_88c697c1877e5045_ea12172c-4b96-4fdc-8398-3e8830a50124=true; ip_city=fuyang; Hm_lpvt_c4e8e5b919a5c12647962ea08462e63b=1544627691
Host: www.91160.com
Upgrade-Insecure-Requests: 1
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_1) AppleWebKit/5327.36 (KHTML, like Gecko) Chrome/70.0.3538.110 Safari/537.36"""#放入自己复制的请求头信息
header = ''
for i in headers:
    if i == ':':
        i = "':'"
    if i == '\n':
        i = "',\n'"
    header += i
print(header[2:].replace(' ', '')+'\'')

运行得出

'Accept':'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8',
'Accept-Encoding':'gzip,deflate,br',
'Accept-Language':'zh-CN,zh;q=0.9',
'Cache-Control':'max-age=0',
'Connection':'keep-alive',
'Cookie':'__jsluid=e0f50b42c0133479609728104ecfa0bdb;_ga=GA1.2.733796313.1544613362;_gid=GA1.2.1036781813.15446313362;Hm_lvt_c4e8e5b919a5c12647962ea08462e63b=1544613362;gr_user_id=29e5f963-4dbb-411a-9233-fb3caf3c7692;is_read_index_notice_=1;__guid=XynHr45c10edf29685b6.52148503;FISKCDDCC=b92099aa55b9f8ab4f431843e799e41d;_nyjy_newadv_=1;SHADOWMAN=%7B%22key%22%3A%2250442c70686cf47a4a6e3196e074f17df%22%2C%22val%22%3A%22e058341874ca0adecba52db27besf94f0%22%2C%22tm%22%3A1544614351%7D;channel_id=16;location_city_id=5;NY_VALIDATE_KEY=c20a31c6de4eaf9ad8b1a0d9670a525a;gr_session_id_88c697c1877e5045=ea12172c-4b96-4fdc-8398-3e8830a50124;gr_session_id_88c697c1877e5045_ea12172c-4b96-4fdc-8398-3e8830a50124=true;ip_city=fuyang;Hm_lpvt_c4e8e5b919a5c12647962ea08462e63b=1544627691',
'Host':'www.91160.com',
'Upgrade-Insecure-Requests':'1',
'User-Agent':'Mozilla/5.0(Macintosh;IntelMacOSX10_12_1)AppleWebKit/5327.36(KHTML,likeGecko)Chrome/70.0.3538.110Safari/537.36'

也可以在ide里面用正则替换,只不过每次都要写正则,麻烦,还是直接复制运行快