happyyangyanghappy

在python中,requests使用代理要比urllib好用太多,urllib还是有些交互性差。

代理

如果需要使用代理,你可以通过为任意请求方法提供 proxies 参数来配置单个请求:

import requests

proxies = {
  "http": "http://10.10.1.10:3128",
  "https": "http://10.10.1.10:1080",
}

requests.get("http://example.org", proxies=proxies)

你也可以通过环境变量 HTTP_PROXYHTTPS_PROXY 来配置代理。

$ export HTTP_PROXY="http://10.10.1.10:3128"
$ export HTTPS_PROXY="http://10.10.1.10:1080"

$ python
>>> import requests
>>> requests.get("http://example.org")

若你的代理需要使用HTTP Basic Auth,可以使用 http://user:password@host/ 语法:

proxies = {
    "http": "http://user:pass@10.10.1.10:3128/",
}

要为某个特定的连接方式或者主机设置代理,使用 scheme://hostname 作为 key, 它会针对指定的主机和连接方式进行匹配。

proxies = {\'http://10.20.1.128\': \'http://10.10.1.10:5323\'}

注意,代理 URL 必须包含连接方式。
参考: http://docs.python-requests.org/zh_CN/latest/user/advanced.html



作者:_小老虎_
链接:https://www.jianshu.com/p/c8f896d668d6
来源:简书
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

分类:

技术点:

相关文章:

  • 2021-12-23
  • 2021-12-23
  • 2021-12-25
  • 2021-12-06
  • 2021-11-23
  • 2021-09-07
  • 2021-11-07
  • 2021-09-28
猜你喜欢
  • 2021-11-09
  • 2021-10-25
  • 2021-12-19
  • 2021-11-02
  • 2019-02-16
  • 2021-11-04
  • 2022-01-02
相关资源
相似解决方案