Python 转换请求头headers的格式

2018-02-27 11:28:38来源:oschina作者:阿豪boy人点击

分享

用Python 将文本类型的请求头格式转化为字典型


文本


User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:58.0) Gecko/20100101 Firefox/58.0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: zh-CN,zh;q=0.8,zh-TW;q=0.7,zh-HK;q=0.5,en-US;q=0.3,en;q=0.2
Accept-Encoding: gzip, deflate, br
Cookie: _ga=GA1.2.138608325.1518712065
Connection: keep-alive
Upgrade-Insecure-Requests: 1
Pragma: no-cache
Cache-Control: no-cache

结果


{
'Host' : 'api.shanbay.com',
'User-Agent' : 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv',
'Accept' : 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
'Accept-Language' : 'zh-CN,zh;q=0.8,zh-TW;q=0.7,zh-HK;q=0.5,en-US;q=0.3,en;q=0.2',
'Accept-Encoding' : 'gzip, deflate, br',
'Cookie' : '_ga=GA1.2.138608325.1518712065',
'Connection' : 'keep-alive',
'Upgrade-Insecure-Requests' : '1',
'Pragma' : 'no-cache',
'Cache-Control' : 'no-cache',
}

基本上就是使用一些简单分割函数


#! D:/Program Files/Python36/python.exe
s = '{/n'
with open("h.txt", mode='r', encoding='utf8') as f:
for i in f.readlines():
i = i.strip()
if i == "":
continue
line = i.split(":")
new_line = "/'" + line[0].strip() + "/' : " + "/'" + line[1].strip() + "/',/n"
s += new_line
s.strip(",")
s += "}"
print(s)
with open("res.txt",mode='w+',encoding='utf8') as f:
f.write(s)

也可以用正则表达式


后面看了再说

上面有点问题,直接使用字符串分割的话,遇到一行有多个:的情况会出现缺失


import re
res = "{/n"
with open('h.txt', encoding='utf8', mode='r') as f:
res += ',/n'.join(
["/t/"" + line[:line.find(':')].strip() + "/" : " + "/"" + line[line.find(":")+1:].strip() + "/""
for line in f]
)
res += "/n}"
print(res)

使用列表生成式配合字符串join方法,先找到第一个:出现的位置,然后根据这个位置将整个字符串截断为两部分,这样就能满足要求


注意使用第一种方法时,rv后面的会缺失


针对整个文档的正则表达式版,key一般比较简单其中没有:符号,所以使用非贪婪匹配,而在val中有时候会有:出现,所以使用贪婪匹配,匹配尽可能多的字符,在:周围使用/s匹配空白字符


import re
with open('h.txt', mode='r', encoding='utf8') as f:
target = f.read()
print(target)
# key使用非贪婪匹配,由于在val中存在:所以val使用贪婪匹配
pattern = '(.*?)/s*:/s*(.*)'
print(
re.sub(pattern,
lambda item:
f"/"{item.group(1)}/" : /"{item.group(2)}/",",
target)
)
"Host" : "api.shanbay.com",
"User-Agent" : "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:58.0) Gecko/20100101 Firefox/58.0",
"Accept" : "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8",
"Accept-Language" : "zh-CN,zh;q=0.8,zh-TW;q=0.7,zh-HK;q=0.5,en-US;q=0.3,en;q=0.2",
"Accept-Encoding" : "gzip, deflate, br",
"Cookie" : "_ga=GA1.2.138608325.1518712065",
"Connection" : "keep-alive",
"Upgrade-Insecure-Requests" : "1",
"Pragma" : "no-cache",
"Cache-Control" : "no-cache",

针对每一行的正则表达式版


import re
with open('h.txt', mode='r', encoding='utf8') as f:
res = [
re.sub('(.*?):/s*(.*)', lambda item: f"'{item.group(1)}' : '{item.group(2)}'", line.strip())
for line in f
]
print(',/n'.join(res))'Host' : 'api.shanbay.com',
'User-Agent' : 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:58.0) Gecko/20100101 Firefox/58.0',
'Accept' : 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
'Accept-Language' : 'zh-CN,zh;q=0.8,zh-TW;q=0.7,zh-HK;q=0.5,en-US;q=0.3,en;q=0.2',
'Accept-Encoding' : 'gzip, deflate, br',
'Cookie' : '_ga=GA1.2.138608325.1518712065',
'Connection' : 'keep-alive',
'Upgrade-Insecure-Requests' : '1',
'Pragma' : 'no-cache',
'Cache-Control' : 'no-cache'

js版


pattern = /(.*?):(.*?)/g
target = `a:1
b:2
c:3
`
console.log(target)
console.log(
target.replace(/(.*):(.*)/n?/g, ($0,$1, $2) => {
// console.log(0,$0)
// console.log(1,$1)
// console.log(2,$2)
return '"'+$1 + '":"' + $2+'",/n'
})
)
a:1
b:2
c:3
"a":"1",
"b":"2",
"c":"3",

最新文章

123

最新摄影

微信扫一扫

第七城市微信公众平台