import json # 使用三引号将浏览器复制出来的requests headers参数赋值给一个变量 headers = """ Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3 Accept-Encoding: gzip, deflate, br Accept-Language: zh-CN,zh;q=0.9,en;q=0.8 Cache-Control: max-age=0 Connection: keep-alive sugstore=1 DNT: 1 Host: www.baidu.com Sec-Fetch-Mode: navigate Sec-Fetch-Site: none Sec-Fetch-User: ?1 Upgrade-Insecure-Requests: 1 User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/77.0.3865.90 Safari/537.36 """ # 去除参数头尾的空格并按换行符分割 headers = headers.strip().split(' ') # 使用字典生成式将参数切片重组,并去掉空格,处理带协议头中的:// headers = {x.split(':')[0].strip(): ("".join(x.split(':')[1:])).strip().replace('//', "://") for x in headers} # 使用json模块将字典转化成json格式打印出来 print(json.dumps(headers, indent=1))