欢迎来到代码驿站!

Python代码

当前位置:首页 > 软件编程 > Python代码

正则给header的冒号两边参数添加单引号(Python请求用)

时间:2020-11-12 08:31:51|栏目:Python代码|点击:

正则给header的冒号两边参数添加单引号(Python请求用)

直接从浏览器Chrome复制header值如下:

Host: kyfw.12306.cn
Connection: keep-alive
Pragma: no-cache
Cache-Control: no-cache
Accept: */*
X-Requested-With: XMLHttpRequest
If-Modified-Since: 0
User-Agent: Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.103 Safari/537.36
Referer: https://kyfw.12306.cn/otn/leftTicket/init?linktypeid=dc&fs=%E4%B8%8A%E6%B5%B7,SHH&ts=%E9%A6%99%E6%B8%AF%E8%A5%BF%E4%B9%9D%E9%BE%99,XJA&date=2019-04-12&flag=N,N,Y
Accept-Encoding: gzip, deflate, br
Accept-Language: zh-CN,zh;q=0.9
Cookie: JSESSIONID=76B27521A27493046570C27D4EA89116; RAIL_OkLJUJ=FFA0Gp4TGPWRINmcFUWWyIHH0ugtMppg; _jc_save_wfdc_flag=dc; ten_key=ySPk59Ew/hagxMnD31qdYQ64ryEVBMwd; ten_js_key=ySPk59Ew%2FhagxMnD31qdYQ64ryEVBMwd; BIGipServerotn=1123025418.64545.0000; RAIL_EXPIRATION=1555320982648; RAIL_DEVICEID=SandqZ9YRixyZOnJ2ZMUl2iYN8RPrDz8F6WsV6wDEryAZh_41c-qHzPyRmNlJNXpOSoNpmYE3SIpEhKVV06mAxHwB6RWk-l4vxZg0an4qePt-qPsrnaceVrdc7eZzC1dJheS9V9TYLwrGQ_iG85ax_hB8VADhhu3; BIGipServerpool_passport=200081930.50215.0000; route=c5c62a339e7744272a54643b3be5bf64; _jc_save_fromStation=%u4E0A%u6D77%2CSHH; _jc_save_toDate=2019-04-12; BIGipServerportal=2949906698.17695.0000; _jc_save_toStation=%u5357%u5B81%2CNNZ; _jc_save_fromDate=2019-04-30

用正则表达式替换后的如下:(正则替换后再手动去掉最后一行最后一个逗号)

'Host':' kyfw.12306.cn',
'Connection':' keep-alive',
'Pragma':' no-cache',
'Cache-Control':' no-cache',
'Accept':' */*',
'X-Requested-With':' XMLHttpRequest',
'If-Modified-Since':' 0',
'User-Agent':' Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.103 Safari/537.36',
'Referer':' https://kyfw.12306.cn/otn/leftTicket/init?linktypeid=dc&fs=%E4%B8%8A%E6%B5%B7,SHH&ts=%E9%A6%99%E6%B8%AF%E8%A5%BF%E4%B9%9D%E9%BE%99,XJA&date=2019-04-12&flag=N,N,Y',
'Accept-Encoding':' gzip, deflate, br',
'Accept-Language':' zh-CN,zh;q=0.9',
'Cookie':' JSESSIONID=76B27521A27493046570C27D4EA89116; RAIL_OkLJUJ=FFA0Gp4TGPWRINmcFUWWyIHH0ugtMppg; _jc_save_wfdc_flag=dc; ten_key=ySPk59Ew/hagxMnD31qdYQ64ryEVBMwd; ten_js_key=ySPk59Ew%2FhagxMnD31qdYQ64ryEVBMwd; BIGipServerotn=1123025418.64545.0000; RAIL_EXPIRATION=1555320982648; RAIL_DEVICEID=SandqZ9YRixyZOnJ2ZMUl2iYN8RPrDz8F6WsV6wDEryAZh_41c-qHzPyRmNlJNXpOSoNpmYE3SIpEhKVV06mAxHwB6RWk-l4vxZg0an4qePt-qPsrnaceVrdc7eZzC1dJheS9V9TYLwrGQ_iG85ax_hB8VADhhu3; BIGipServerpool_passport=200081930.50215.0000; route=c5c62a339e7744272a54643b3be5bf64; _jc_save_fromStation=%u4E0A%u6D77%2CSHH; _jc_save_toDate=2019-04-12; BIGipServerportal=2949906698.17695.0000; _jc_save_toStation=%u5357%u5B81%2CNNZ; _jc_save_fromDate=2019-04-30'

find匹配:(.*?)\:(.*?)\n

replace替换:'$1':'$2',\n

sublime如下:

匹配效果如下:


总结

上一篇:Python比较2个时间大小的实现方法

栏    目:Python代码

下一篇:Python3.5 win10环境下导入kera/tensorflow报错的解决方法

本文标题:正则给header的冒号两边参数添加单引号(Python请求用)

本文地址:http://www.codeinn.net/misctech/21795.html

推荐教程

广告投放 | 联系我们 | 版权申明

重要申明:本站所有的文章、图片、评论等,均由网友发表或上传并维护或收集自网络,属个人行为,与本站立场无关。

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:914707363 | 邮箱:codeinn#126.com(#换成@)

Copyright © 2020 代码驿站 版权所有