将代理设置为urlib.request(Python3)

gm1*_*gm1 4 python proxy urllib

如何urllib在Python 3中为最后一个设置代理.我正在做下一个

from urllib import request as urlrequest
ask = urlrequest.Request(url)     # note that here Request has R not r as prev versions
open = urlrequest.urlopen(req)
open.read()
Run Code Online (Sandbox Code Playgroud)

我尝试添加代理如下:

ask=urlrequest.Request.set_proxy(ask,proxies,'http')
Run Code Online (Sandbox Code Playgroud)

但是,由于我收到下一个错误,我不知道它是多么正确:

336     def set_proxy(self, host, type):
--> 337         if self.type == 'https' and not self._tunnel_host:
    338             self._tunnel_host = self.host
    339         else:

AttributeError: 'NoneType' object has no attribute 'type'
Run Code Online (Sandbox Code Playgroud)

mha*_*wke 11

你应该调用类set_proxy()实例Request,而不是类本身:

from urllib import request as urlrequest

proxy_host = 'localhost:1234'    # host and port of your proxy
url = 'http://www.httpbin.org/ip'

req = urlrequest.Request(url)
req.set_proxy(proxy_host, 'http')

response = urlrequest.urlopen(req)
print(response.read().decode('utf8'))
Run Code Online (Sandbox Code Playgroud)


Ale*_*orb 8

我需要在我们公司的环境中禁用代理,因为我想访问本地主机上的服务器。我无法使用@mhawke 的方法禁用代理服务器(试图通过{}None[]作为代理)。

这对我有用(也可用于设置特定代理,请参阅代码中的注释)。

import urllib.request as request

# disable proxy by passing an empty
proxy_handler = request.ProxyHandler({})
# alertnatively you could set a proxy for http with
# proxy_handler = request.ProxyHandler({'http': 'http://www.example.com:3128/'})

opener = request.build_opener(proxy_handler)

url = 'http://www.example.org'

# open the website with the opener
req = opener.open(url)
data = req.read().decode('utf8')
print(data)
Run Code Online (Sandbox Code Playgroud)


Pie*_*erz 5

Urllib 将自动检测环境中设置的代理HTTP_PROXY- 因此人们可以在您的环境中设置变量,例如 Bash:

export HTTP_PROXY=http://proxy_url:proxy_port
Run Code Online (Sandbox Code Playgroud)

或使用Python例如

import os
os.environ['HTTP_PROXY'] = 'http://proxy_url:proxy_port'
Run Code Online (Sandbox Code Playgroud)

urllib 文档中的注释:“HTTP_PROXY如果设置了变量,[环境变量]将被忽略;请参阅有关getproxiesREQUEST_METHOD ()的文档”

  • 根据我的经验, os.environ 有正确的代理设置,但 urllib 没有选择它。我必须使用 urllib.request.ProxyHandler 显式设置它才能使其工作。 (2认同)