python urllib.urlopen 不起作用。有解决方法吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1076958/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 21:25:30  来源:igfitidea点击:

urllib.urlopen isn't working. Is there a workaround?

pythonurl

提问by mandroid

I'm getting a getaddress error and after doing some sleuthing, it looks like it might be my corporate intranet not allowing the connection (I'm assuming due to security, although it is strange that IE works but won't allow Python to open a url). Is there a safe way to get around this?

我收到了一个 getaddress 错误,在做了一些调查之后,看起来可能是我的公司内部网不允许连接(我假设是出于安全考虑,虽然 IE 可以工作但不允许 Python 打开很奇怪)一个网址)。有没有安全的方法来解决这个问题?

Here's the exact error:

这是确切的错误:

Traceback (most recent call last):
  File "<pyshell#1>", line 1, in <module>
    b = urllib.urlopen('http://www.google.com')
  File "C:\Python26\lib\urllib.py", line 87, in urlopen
    return opener.open(url)
  File "C:\Python26\lib\urllib.py", line 203, in open
    return getattr(self, name)(url)
  File "C:\Python26\lib\urllib.py", line 342, in open_http
    h.endheaders()
  File "C:\Python26\lib\httplib.py", line 868, in endheaders
    self._send_output()
  File "C:\Python26\lib\httplib.py", line 740, in _send_output
    self.send(msg)
  File "C:\Python26\lib\httplib.py", line 699, in send
    self.connect()
  File "C:\Python26\lib\httplib.py", line 683, in connect
    self.timeout)
  File "C:\Python26\lib\socket.py", line 498, in create_connection
    for res in getaddrinfo(host, port, 0, SOCK_STREAM):
IOError: [Errno socket error] [Errno 11001] getaddrinfo failed

More info: I also get this error with urllib2.urlopen

更多信息:我也收到了 urllib2.urlopen 这个错误

回答by Unknown

You probably need to fill in proxy information.

您可能需要填写代理信息。

import urllib2
proxy_handler = urllib2.ProxyHandler({'http': 'http://yourcorporateproxy:12345/'})
proxy_auth_handler = urllib2.HTTPBasicAuthHandler()
proxy_auth_handler.add_password('realm', 'host', 'username', 'password')

opener = urllib2.build_opener(proxy_handler, proxy_auth_handler)
opener.open('http://www.stackoverflow.com')

回答by rob

Check you are using the correct proxy.
You can get the proxy information by using urllib.getproxies (note: getproxies does notwork with dynamic proxy configuration, like when using PAC).

检查您使用的是正确的代理。
您可以通过使用urllib.getproxies获得代理信息(注:getproxies确实使用动态代理配置,使用PAC时像工作)。

UpdateAs per information about empty proxy list, I would suggest using an urlopener, with the proxy name and information.
Some good information about how use proxies urlopeners:

更新根据有关空代理列表的信息,我建议使用带有代理名称和信息的 urlopener。
关于如何使用代理 urlopeners 的一些很好的信息:

  1. Urllib manual
  2. Michael Foord's introduction to urllib
  1. urllib 手册
  2. Michael Foord 对 urllib 的介绍

回答by mhawke

Possibly this is a DNS issue, try urlopen with the IP address of the web server you're accessing, i.e.

可能这是 DNS 问题,请尝试使用您正在访问的 Web 服务器的 IP 地址 urlopen,即

import urllib
URL="http://66.102.11.99"   # www.google.com
f = urllib.urlopen(URL)
f.read()

If this succeeds, then it's probably a DNS issue rather than a proxy issue (but you should also check your proxy setup).

如果成功,那么它可能是 DNS 问题而不是代理问题(但您还应该检查您的代理设置)。

回答by Anthony Kong

Looks like a DNS problem.

看起来像一个DNS问题。

Since you are using Windows, you can try run this command

由于您使用的是 Windows,您可以尝试运行此命令

nslookup www.google.com

To check if the web address can be resolved successfully.

检查网址是否可以成功解析。

If not, it is a network setting issue

如果不是,则是网络设置问题

If OK, then we have to look at possible alternative causes

如果正常,那么我们必须查看可能的替代原因

回答by Aman Aggarwal

I was facing the same issue. In my system the proxy configuration is through a .PAC file. So i opended that file, took out the default proxy url, for me it was http://168.219.61.250:8080/

我面临同样的问题。在我的系统中,代理配置是通过 .PAC 文件进行的。所以我打开那个文件,取出默认的代理 url,对我来说是http://168.219.61.250:8080/

Following test code worked for me :

以下测试代码对我有用:

import urllib2

proxy_support = urllib2.ProxyHandler({'http': 'http://168.219.61.250:8080/'})
opener = urllib2.build_opener(proxy_support)
urllib2.install_opener(opener)
response = urllib2.urlopen('http://python.org/')
html = response.read()
print html

You might need to add some more code, if your proxy requires authentication

如果您的代理需要身份验证,您可能需要添加更多代码

Hope this helps!!

希望这可以帮助!!