IOError: [错误号 url 错误] 无效的 http 代理 'xxx.xxx.xxx.xxx

3 投票
3 回答
6910 浏览
提问于 2025-04-16 00:54

我在用我的脚本时遇到了一些麻烦。这个脚本本来是要通过代理打开一个网站的,但我总是收到这个错误信息,尝试了好几个代理都是这样...

这可能是什么原因呢?

Traceback (most recent call last):
  File "C:\Users\Shady\Desktop\ptzplace.3.0 - Copy.py", line 43, in <module>
    h = urllib.urlopen(website, proxies = {'http': proxy})
  File "C:\Python26\lib\urllib.py", line 86, in urlopen
    return opener.open(url)
  File "C:\Python26\lib\urllib.py", line 200, in open
    return self.open_unknown_proxy(proxy, fullurl, data)
  File "C:\Python26\lib\urllib.py", line 219, in open_unknown_proxy
    raise IOError, ('url error', 'invalid proxy for %s' % type, proxy)
IOError: [Errno url error] invalid proxy for http: 'xxx.xxx.xxx.xxx'

这个脚本是这样的

proxylist = ['79.174.195.84:80',
             '79.174.195.82:80',
             '80.233.184.227:8080',
             '79.174.195.80:80',
             '80.233.184.226:8080',
             '79.174.33.95:3128']
for proxy in proxylist:
            h = urllib.urlopen(website, proxies = {'http': proxy})

3 个回答

5

我觉得在每个代理前面,你需要加上http这个标识。

proxylist = ['http://79.174.195.84:80',... 'http://79.174.33.95:3128'] 
9

试试用 urllib2.urlopen,而不是 urllib.urlopen。我遇到过一些情况,urllib.urlopen 在我的代理服务器上出问题,但 urllib2.urlopen 就能正常打开。

3

我试了你提供的代理列表里的代码,但花了很长时间,所以我换了其他的代理 :P

import urllib
website = 'http://www.google.com/'
proxylist = ('http://75.101.215.123:9090', 'http://94.198.47.6:3128')
connlist = (urllib.urlopen(website, proxies = {'http': proxy}) for proxy in proxylist)
for conn in connlist:
    print conn.read()
    conn.close()

撰写回答