代码之家  ›  专栏  ›  技术社区  ›  SanthoshSolomon

HTTP头-请求-python

  •  0
  • SanthoshSolomon  · 技术社区  · 8 年前

    我正在尝试搜集一个网站,其中请求头具有一些新的(对我而言)属性,例如 :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':方案'

    如有任何帮助,请理解并指导如何在通过请求时使用它们。

    编辑: 添加代码

    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)
    
    2 回复  |  直到 8 年前
        1
  •  2
  •   Nikos Vita Topiko    8 年前

    你的错误来自 here (python的源代码)

    由于RFC状态,HTTP头不能以分号开头。

        2
  •  1
  •   Luk    8 年前