python requests headers自動生成
阿新 • • 發佈:2018-12-15
def str2header(headers_raw): if headers_raw is None: return None headers = headers_raw.splitlines() headers_tuples = [header.split(':', 1) for header in headers] result_dict = {} for header_item in headers_tuples: if not len(header_item) == 2: continue item_key = header_item[0].strip() item_value = header_item[1].strip() result_dict[item_key] = item_value return result_dict #抓包看到的header字串 r_h = ''' GET / HTTP/1.1 Host: www.baidu.com Connection: keep-alive Cache-Control: max-age=0 Upgrade-Insecure-Requests: 1 User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) 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,en;q=0.8 Cookie: *** ''' #headers已經生成 可直接在requests.get(url,headers=headers) headers=str2header(r_h)