python处理headers,data,params冒号两侧参数,自动添加单引号和逗号

学习爬虫的时候,碰到headers,params,和data等,每次都要一个一个加引号,还有每一行行尾都要加逗号,很麻烦。于是自己写了一个方法,使用的时候,把内容复制粘贴进去运行,再把打印结果复制出来就行。

代码如下:

import re

content = '''
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3
Accept-Encoding: gzip, deflate, br
Accept-Language: zh-CN,zh;q=0.9
Cache-Control: max-age=0
Connection: keep-alive
Host: www.baidu.com
Sec-Fetch-Mode: navigate
Sec-Fetch-Site: none
Sec-Fetch-User: ?1
Upgrade-Insecure-Requests: 1
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/535.1 (KHTML, like Gecko) Chrome/14.0.835.163 Safari/535.1
'''
# 每次只要把content的内容换掉就行

text0 = re.findall('(.*?)\:(.*?)\n',content)
#print(text0)
text_final =''
for i in text0:
    text1 = '\''+i[0]+'\':\''+i[1]+'\',\n'
    text_final = text_final +text1
print(text_final.replace(' ',''))

运行结果如下:

'Accept':'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3',
'Accept-Encoding':'gzip,deflate,br',
'Accept-Language':'zh-CN,zh;q=0.9',
'Cache-Control':'max-age=0',
'Connection':'keep-alive',
'Host':'www.baidu.com',
'Sec-Fetch-Mode':'navigate',
'Sec-Fetch-Site':'none',
'Sec-Fetch-User':'?1',
'Upgrade-Insecure-Requests':'1',
'User-Agent':'Mozilla/5.0(WindowsNT6.1;WOW64)AppleWebKit/535.1(KHTML,likeGecko)Chrome/14.0.835.163Safari/535.1',

复制粘贴时,需要把最后一个","手动忽略

你可能感兴趣的:(python处理headers,data,params冒号两侧参数,自动添加单引号和逗号)