编写高质量代码:改善Python程序的91个建议中文pdf扫描版[54MB]
正则给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如下:
匹配效果如下:
总结
以上所述是小编给大家介绍的正则给header的冒号两边参数添加单引号(Python请求用),希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对谷谷点程序网站的支持!
如果你觉得本文对你有帮助,欢迎转载,烦请注明出处,谢谢!