当前位置: 首页 > 最新文章 > 正文

Python-快速添加headers请求头

从网页里复制的 请求头,在复制到pycharm中时,格式不一样,每次都需要慢慢的调整,后来通过re正则表达式快速处理。1Upgrade-Insecure-Requests: 1User-Agent: Mozilla/5.0 AppleWebKit/537.36 Chrome/92.0.4515.107 Safari/537.36 Edg/92.0.902.62通过以下正则表达式后会变成:imp

admin

从网页里复制的 请求头,在复制到pycharm中时,格式不一样,每次都需要慢慢的调整,后来通过re正则表达式快速处理。1Upgrade-Insecure-Requests: 1User-Agent: Mozilla/5.0 AppleWebKit/537.36 Chrome/92.0.4515.107 Safari/537.36 Edg/92.0.902.62通过以下正则表达式后会变成:import re# 下方引号内添加替换掉请求头内容headers_str = """Accept-Encoding: gzip, deflate, brAccept-Language: zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6Cache-Control: no-cacheConnection: keep-aliveCookie: BIDUPSID=CE2731xUjJ4RGNlYTJaWW9razah058k25vo1ggcdpl0qDNT: 1"""pattern = '^(.*?):(.*)#39;for line in headers_str.splitlines(): print最终结果'Accept':'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9','Accept-Encoding':'gzip,deflate,br','Accept-Language':'zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6','Cache-Control':'no-cache','Connection':'keep-alive','Cookie':'BIDUPSID=CE2731xUjJ4RGNlYTJaWW9razah058k25vo1ggcdpl0q','DNT':'1','Host':'www.baidu.com','Pragma':'no-cache','sec-ch-ua':'"Chromium";v="92","NotA;Brand";v="99","MicrosoftEdge";v="92"','sec-ch-ua-mobile':'?

从网页里复制的 请求头,在复制到pycharm中时,格式不一样,每次都需要慢慢的调整,后来通过re正则表达式快速处理。

比如原来的是

Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9Accept-Encoding: gzip, deflate, brAccept-Language: zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6Cache-Control: no-cacheConnection: keep-aliveCookie: BIDUPSID=CE2731xUjJ4RGNlYTJaWW9razah058k25vo1ggcdpl0qDNT: 1Host: www.baidu.comPragma: no-cachesec-ch-ua: "Chromium";v="92", " Not A;Brand";v="99", "Microsoft Edge";v="92"sec-ch-ua-mobile: ?0Sec-Fetch-Dest: documentSec-Fetch-Mode: navigateSec-Fetch-Site: noneSec-Fetch-User: ?1Upgrade-Insecure-Requests: 1User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.107 Safari/537.36 Edg/92.0.902.62

通过以下正则表达式后会变成:

import re# 下方引号内添加替换掉请求头内容headers_str = """Accept-Encoding: gzip, deflate, brAccept-Language: zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6Cache-Control: no-cacheConnection: keep-aliveCookie: BIDUPSID=CE2731xUjJ4RGNlYTJaWW9razah058k25vo1ggcdpl0qDNT: 1"""pattern = '^(.*?):(.*)#39;for line in headers_str.splitlines():  print(re.sub(pattern,'\'\\1\':\'\\2\',',line).replace(' ',''))

最终结果

'Accept':'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9','Accept-Encoding':'gzip,deflate,br','Accept-Language':'zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6','Cache-Control':'no-cache','Connection':'keep-alive','Cookie':'BIDUPSID=CE2731xUjJ4RGNlYTJaWW9razah058k25vo1ggcdpl0q','DNT':'1','Host':'www.baidu.com','Pragma':'no-cache','sec-ch-ua':'"Chromium";v="92","NotA;Brand";v="99","MicrosoftEdge";v="92"','sec-ch-ua-mobile':'?0','Sec-Fetch-Dest':'document','Sec-Fetch-Mode':'navigate','Sec-Fetch-Site':'none','Sec-Fetch-User':'?1','Upgrade-Insecure-Requests':'1','User-Agent':'Mozilla/5.0(WindowsNT10.0;Win64;x64)AppleWebKit/537.36(KHTML,likeGecko)Chrome/92.0.4515.107Safari/537.36Edg/92.0.902.6',Process finished with exit code 0


上一篇: 手机系统手机系统(手机端系统) 下一篇:数据库大师成长日记:经典SQL语句大全,基础部分,敬请收藏!
返回顶部