我們?cè)趯懪老x腳本的時(shí)候經(jīng)常要獲取請(qǐng)求頭巫财,但是每次從瀏覽器粘貼到代碼里時(shí)盗似,都要費(fèi)一番功夫來處理格式。
于是寫了一個(gè)請(qǐng)求頭轉(zhuǎn)換的腳本平项,可以將瀏覽器里復(fù)制過來的請(qǐng)求頭字符串轉(zhuǎn)換為字典并輸出赫舒。
import re
def headers_to_dict(headers_str, out_put=True):
items = headers_str.strip().split('\n')
headers_dict = {}
for t in items:
key, value = re.findall(r'^(\S+):\s*([\s\S]+)$', t)[0]
headers_dict[key] = value
if out_put:
print(f"'{key}': '{value}',")
return headers_dict
使用說明:
-
headers_str
從瀏覽器復(fù)制的請(qǐng)求頭字符串,使用三個(gè)單引號(hào) -
out_put
是否輸出格式化的字符串闽瓢,為 True時(shí)會(huì)將每個(gè)鍵值對(duì)以'key':'value',
的格式輸出接癌,可以直接粘貼到字典中。覺得換行字符串不好看的可以用這個(gè)將請(qǐng)求頭輸出扣讼,然后手動(dòng)粘貼到代碼中缺猛。 - 返回值,字符串對(duì)應(yīng)的請(qǐng)求頭字典椭符。
使用示例:
headers_to_dict(''':authority: www.reibang.com
:method: GET
:path: /p/b671f67a5960
:scheme: https
accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8
accept-encoding: gzip, deflate, br
accept-language: zh-CN,zh;q=0.9
cache-control: max-age=0
if-none-match: W/"0d1384f05bc47dfa8d8d26187e1b3f4f"
referer: http://www.reibang.com/writer
upgrade-insecure-requests: 1
user-agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.98 Safari/537.36''')
#輸出
"""
':authority': 'www.reibang.com',
':method': 'GET',
':path': '/p/b671f67a5960',
':scheme': 'https',
'accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8',
'accept-encoding': 'gzip, deflate, br',
'accept-language': 'zh-CN,zh;q=0.9',
'cache-control': 'max-age=0',
'if-none-match': 'W/"0d1384f05bc47dfa8d8d26187e1b3f4f"',
'referer': 'http://www.reibang.com/writer',
'upgrade-insecure-requests': '1',
'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.98 Safari/537.36',
"""
#返回值
"""
{
':authority': 'www.reibang.com',
':method': 'GET',
':path': '/p/b671f67a5960',
':scheme': 'https',
'accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8',
'accept-encoding': 'gzip, deflate, br',
'accept-language': 'zh-CN,zh;q=0.9',
'cache-control': 'max-age=0',
'if-none-match': 'W/"0d1384f05bc47dfa8d8d26187e1b3f4f"',
'referer': 'http://www.reibang.com/writer',
'upgrade-insecure-requests': '1',
'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.98 Safari/537.36'
}
"""