header python 环境信息_Python 转换请求头headers的格式

本文介绍了如何使用Python将文本格式的HTTP请求头转换为字典类型,包括通过简单的字符串分割方法,正则表达式以及JavaScript实现。展示了不同方法的代码示例,适合需要处理HTTP头信息的开发者参考。
摘要由CSDN通过智能技术生成

用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后面的会缺失

aa985d145599384066e93af5c07e37e7.png

针对整个文档的正则表达式版,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",

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值