在这个脚本上获得大量错误?

时间:2014-08-30 01:41:44

标签: python proxy urllib2

我已经在这里待了45分钟。我迫切需要帮助。

当我运行时:

import urllib, urllib2

proxy = urllib2.ProxyHandler({
    'http': '127.0.0.1',
    'https': '127.0.0.1'
})
opener = urllib2.build_opener(proxy)
urllib2.install_opener(opener)
# this way both http and https requests go through the proxy
urllib2.urlopen('http://www.google.com')
urllib2.urlopen('https://www.google.com')

我明白了:

Traceback (most recent call last):
  File "C:\Python27\Craig.py", line 1, in <module>
    import urllib, urllib2
  File "C:\Python27\lib\urllib2.py", line 94, in <module>
    import httplib
  File "C:\Python27\lib\httplib.py", line 79, in <module>
    import mimetools
  File "C:\Python27\lib\mimetools.py", line 6, in <module>
    import tempfile
  File "C:\Python27\lib\tempfile.py", line 35, in <module>
    from random import Random as _Random
  File "C:\Python27\random.py", line 3, in <module>
    print random.randint(0, 1) # prints either 0 or 1  (for me it printed 0)
AttributeError: 'module' object has no attribute 'randint'

更新:

修正了random.py问题,现在得到:

  File "C:\Python27\Craig.py", line 10, in <module>
    urllib2.urlopen('http://www.google.com')
  File "C:\Python27\lib\urllib2.py", line 127, in urlopen
    return _opener.open(url, data, timeout)
  File "C:\Python27\lib\urllib2.py", line 410, in open
    response = meth(req, response)
  File "C:\Python27\lib\urllib2.py", line 523, in http_response
    'http', request, response, code, msg, hdrs)
  File "C:\Python27\lib\urllib2.py", line 448, in error
    return self._call_chain(*args)
  File "C:\Python27\lib\urllib2.py", line 382, in _call_chain
    result = func(*args)
  File "C:\Python27\lib\urllib2.py", line 531, in http_error_default
    raise HTTPError(req.get_full_url(), code, msg, hdrs, fp)
urllib2.HTTPError: HTTP Error 501: Not Implemented

1 个答案:

答案 0 :(得分:1)

当前目录中有一个名为random.py的文件。将其重命名为random_.py

之类的其他内容

注意,使用C:\Python27中的文件进行开发可能不是一个好习惯。