python3.5里面 urlopen 代理不能识别proxies关键字的问题

urllib.request 里面 的 urlopen  proxies={‘http’:proxies} 无法识别出现 下列错误如何解决

got an unexpected keyword argument 'proxies'

首先通过 cmd,用命令 python -v

找到   C:\Users\computorname\AppData\Local\Programs\Python\Python35\lib

这个位置就是 python的 源码函数类库的存储位置

用sublime或其他编辑器 查找上面的文件夹, 查找内容为 def urlopen

C:\Users\computorname\AppData\Local\Programs\Python\Python35\Lib\site-packages\pip\_vendor\requests\packages\urllib3\connectionpool.py:
  434          return (scheme, host, port) == (self.scheme, self.host, self.port)
  435  
  436:     def urlopen(self, method, url, body=None, headers=None, retries=None,
  437                  redirect=True, assert_same_host=True, timeout=_Default,
  438                  pool_timeout=None, release_conn=None, **response_kw):

C:\Users\computorname\AppData\Local\Programs\Python\Python35\Lib\site-packages\pip\_vendor\requests\packages\urllib3\contrib\appengine.py:
   86          return False
   87  
   88:     def urlopen(self, method, url, body=None, headers=None,
   89                  retries=None, redirect=True, timeout=Timeout.DEFAULT_TIMEOUT,
   90                  **response_kw):

C:\Users\computorname\AppData\Local\Programs\Python\Python35\Lib\site-packages\pip\_vendor\requests\packages\urllib3\contrib\ntlmpool.py:
  105          return conn
  106  
  107:     def urlopen(self, method, url, body=None, headers=None, retries=3,
  108                  redirect=True, assert_same_host=True):
  109          if headers is None:

C:\Users\computorname\AppData\Local\Programs\Python\Python35\Lib\site-packages\pip\_vendor\requests\packages\urllib3\poolmanager.py:
  140          return self.connection_from_host(u.host, port=u.port, scheme=u.scheme)
  141  
  142:     def urlopen(self, method, url, redirect=True, **kw):
  143          """
  144          Same as :meth:`urllib3.connectionpool.HTTPConnectionPool.urlopen`
  ...
  264          return headers_
  265  
  266:     def urlopen(self, method, url, redirect=True, **kw):
  267          "Same as HTTP(S)ConnectionPool.urlopen, ``url`` must be absolute."
  268          u = parse_url(url)

C:\Users\computorname\AppData\Local\Programs\Python\Python35\Lib\site-packages\pip\_vendor\requests\packages\urllib3\request.py:
   45          self.headers = headers or {}
   46  
   47:     def urlopen(self, method, url, body=None, headers=None,
   48                  encode_multipart=True, multipart_boundary=None,
   49                  **kw):  # Abstract

C:\Users\computorname\AppData\Local\Programs\Python\Python35\Lib\test\test_urllib.py:
   34  
   35  
   36: def urlopen(url, data=None, proxies=None):
   37      """urlopen(url [, data]) -> open file-like object"""
   38      global _urlopener

C:\Users\computorname\AppData\Local\Programs\Python\Python35\Lib\test\test_urllib2_localnet.py:
  451          super(TestUrlopen, self).tearDown()
  452  
  453:     def urlopen(self, url, data=None, **kwargs):
  454          l = []
  455          f = urllib.request.urlopen(url, data, **kwargs)

C:\Users\computorname\AppData\Local\Programs\Python\Python35\Lib\test\test_urllibnet.py:
   48  
   49      @contextlib.contextmanager
   50:     def urlopen(self, *args, **kwargs):
   51          resource = args[0]
   52          with support.transient_internet(resource):

C:\Users\computorname\AppData\Local\Programs\Python\Python35\Lib\urllib\request.py:
  138  
  139  _opener = None
  140: def urlopen(url, data=None, timeout=socket._GLOBAL_DEFAULT_TIMEOUT,
  141              *, cafile=None, capath=None, cadefault=False, context=None):
  142      global _opener

10 matches across 9 files

发现上面 test/testurllib.py里面有 proxies关键字

就导入这个类库即可 from test.test_urllib import urlopen

from contextlib import closing
# from urllib.request import urlopen
from test.test_urllib import urlopen
# import urllib
# import requests
proxyConfig="http://%s:%s@%s"%("username","password","10.110.4.235:8080")
print(proxyConfig)
with closing(urlopen("https://www.python.org",proxies={'http':proxyconfig})) as page:
    for line in page:
        print(line)



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
`urlopen`是Python标准库中用于打开URL的函数。在Python3中,它可以通过`urllib.request.urlopen()`来调用。 如果你遇到了`urlopen`报错的问题,以下是一些可能的解决方法: 1. 检查URL是否正确:首先确保你要打开的URL是正确的。如果URL不存在或无法访问,那么`urlopen`函数将会报错。 2. 检查网络连接:如果你的网络连接不稳定或中断,那么`urlopen`函数也会报错。确保你的网络连接正常。 3. 添加User-Agent:有些网站需要在请求头中添加User-Agent才能访问。你可以通过在`urllib.request.Request`中添加headers参数来添加User-Agent。示例代码如下: ```python import urllib.request url = "http://www.example.com" req = urllib.request.Request(url, headers={'User-Agent': 'Mozilla/5.0'}) response = urllib.request.urlopen(req) ``` 4. 添加超时时间:如果请求时间过长,那么`urlopen`函数也会报错。你可以通过在`urlopen`函数中添加timeout参数来设置超时时间。示例代码如下: ```python import urllib.request url = "http://www.example.com" response = urllib.request.urlopen(url, timeout=5) ``` 5. 检查代理设置:如果你使用了代理服务器来访问网站,那么请确保代理设置正确。你可以通过在`urllib.request.ProxyHandler`中指定代理服务器来设置代理。示例代码如下: ```python import urllib.request proxy_handler = urllib.request.ProxyHandler({'http': 'http://www.example.com:8080/'}) opener = urllib.request.build_opener(proxy_handler) response = opener.open('http://www.example.com') ``` 希望以上方法能帮助你解决`urlopen`报错的问题

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值