Py学习  »  问与答

python中使用代理怎么老是出错,这是为毛?

cumt_ttr • 9 年前 • 11131 次点击  
proxy = 'http://127.0.0.1:80'
opener = urllib2.build_opener(urllib2.ProxyHandler({'http': proxy}))
req = urllib2.Request("http://www.baidu.com", '', headers)
urllib2.install_opener(opener)
response = urllib2.urlopen(req).read()
with open('j:/01' + ".srt", "wb") as subtitle:
    subtitle.write(response)

出错信息如下

"I:\Program Files\python27\python.exe" C:/test.py
Traceback (most recent call last):
  File "C:/test.py", line 13, in <module>
    response = urllib2.urlopen(req).read()
  File "I:\Program Files\python27\lib\urllib2.py", line 127, in urlopen
    return _opener.open(url, data, timeout)
  File "I:\Program Files\python27\lib\urllib2.py", line 404, in open
    response = self._open(req, data)
  File "I:\Program Files\python27\lib\urllib2.py", line 422, in _open
    '_open', req)
  File "I:\Program Files\python27\lib\urllib2.py", line 382, in _call_chain
    result = func(*args)
  File "I:\Program Files\python27\lib\urllib2.py", line 1214, in http_open
    return self.do_open(httplib.HTTPConnection, req)
  File "I:\Program Files\python27\lib\urllib2.py", line 1184, in do_open
    raise URLError(err)
urllib2.URLError: <urlopen error [Errno 10061] >

Process finished with exit code 1
Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/622
 
11131 次点击  
文章 [ 3 ]  |  最新文章 9 年前
Py站长
Reply   •   1 楼
Py站长    9 年前

代理IP有问题?

cumt_ttr
Reply   •   2 楼
cumt_ttr    9 年前
def sub_downloader():
    hash1 = r'ffd8d4aa68033dc03d1c8ef373b9028c'

    headers = {
        'User-Agent':
            'SubDB/1.0 (subtitle-downloader-mabing/1.0; http://github.com/manojmj92/subtitle-downloader-mabing)'}
    #u = "http://api.thesubdb.com/?action=download&hash=%s&language=en" % (urllib.quote(hash1))
    u = "http://api.thesubdb.com/?action=download&hash=ffd8d4aa68033dc03d1c8ef373b9028c&language=en"
    if pyVer == 3:
        proxies = {'http': 'http://24.143.198.188:80'}
        #filehandle = urllib.urlopen(some_url, proxies=proxies)
        #req = urllib.request.Request(url, None, headers)

    response = urllib.urlopen(u, proxies=proxies).read()
    #else:
    #url   = ‘http://www.vimer.cn/’
    #proxy = ‘xxx.xxxx.com:8080′
    #opener = urllib2.build_opener( urllib2.ProxyHandler({‘http’:proxy}) )
    #urllib2.install_opener( opener )
    #sContent = urllib2.urlopen(url)
    #proxy = '24.143.198.188:80'
    #opener = urllib2.build_opener(urllib2.ProxyHandler({'http': proxy}))
    #req = urllib2.Request("http://www.baidu.com", '', headers)
    #urllib2.install_opener(opener)
    #response = urllib2.urlopen(req).read()
    with open('j:/01' + ".srt", "wb") as subtitle:
        subtitle.write(response)

现在改成了这样,文件内容变成这样了 <HTML><HEAD> <TITLE>Invalid URL</TITLE> </HEAD><BODY> <H1>Invalid URL</H1>

The requested URL "&#47;&#63;action&#61;download&#38;hash&#61;ffd8d4aa68033dc03d1c8ef373b9028c&#38;language&#61;en", is invalid.<p>
Reference&#32;&#35;9&#46;bcc68f18&#46;1408796874&#46;22f563
</BODY></HTML>
cumt_ttr
Reply   •   3 楼
cumt_ttr    9 年前

不用代理,这段代码是没问题的,请教这个跟我的本机的什么设置有关系么?