【问题标题】:Python requests add header with openerPython 请求使用开启器添加标头
【发布时间】:2019-10-30 09:55:44
【问题描述】:

我想在这个 Python 请求代码中添加自定义标头:

import six
from six.moves.urllib import request
opener = request.build_opener(
request.ProxyHandler(
    {'http': 'http://127.0.0.1:9000'}))
print(opener.open('http://example.com/data.json').read())

打开的 URL 读取自定义标头以适当地回复请求。

【问题讨论】:

    标签: python python-2.7 python-requests


    【解决方案1】:

    我是这样做的:

    import requests as r
    url = "http://example.com/data.json"
    header = {"myheader": 123456}
    proxies = {
     "http": "http://127.0.0.1:9000",
     "https": "http://127.0.0.1:9000",
    }
    data = r.get(url,headers=header,proxies=proxies)
    data = data.json()
    print(data)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-11-27
      • 2016-12-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-03-29
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多