[英]HTTP headers - Requests - Python
我正在嘗試抓取一個網站,其中請求標頭具有一些新的(對我來說)屬性,例如:authority, :method, :path, :scheme
。
{':authority':'xxxx',':method':'GET',':path':'/xxxx',':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':'en-US,en;q=0.9','cache-control':'max-age=0',GOOGLE_ABUSE_EXEMPTION=ID=0d5af55f1ada3f1e:TM=1533116294:C=r:IP=182.71.238.62-:S=APGng0u2o9IqL5wljH2o67S5Hp3hNcYIpw;1P_JAR=2018-8-1-9', 'upgrade-insecure-requests': '1', 'user-agent': 'Mozilla/5.0(WindowsNT6.1;Win64;x64)AppleWebKit/537.36(KHTML,likeGecko)Chrome/68.0.3440.84Safari/537.36', 'x-client-data': 'CJG2yQEIpbbJAQjEtskBCKmdygEI2J3KAQioo8oBCIKkygE=' }
我嘗試將它們作為帶有HTTP請求的標頭傳遞,但最終出現錯誤,如下所示。
ValueError:無效的標題名稱b':scheme'
在理解和指導在傳遞請求時使用它們的任何幫助將不勝感激。
編輯:添加代碼
import requests
url = 'https://www.google.co.in/search?q=some+text'
headers = {':authority':'xxxx',':method':'GET',':path':'/xxxx',':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':'en-US,en;q=0.9','cache-control':'max-age=0','upgrade-insecure-requests': '1', 'user-agent': 'Mozilla/5.0(WindowsNT6.1;Win64;x64)AppleWebKit/537.36(KHTML,likeGecko)Chrome/68.0.3440.84Safari/537.36', 'x-client-data': 'CJG2yQEIpbbJAQjEtskBCKmdygEI2J3KAQioo8oBCIKkygE=' }
response = requests.get(url, headers=headers)
print(response.text)
您的錯誤來自此處 (python的源代碼)
RFC指出,Http標頭不能以分號開頭。
:authority,:method,:path,:scheme不是HTTP標頭
https://zh.wikipedia.org/wiki/List_of_HTTP_header_fields
':method':'GET'
定義http請求方法
https://zh.wikipedia.org/wiki/Hypertext_Transfer_Protocol#Request_methods
和
:authority, :path, :scheme
是URI的一部分https://en.wikipedia.org/wiki/Uniform_Resource_Identifier#Generic_syntax
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.