python-3.x-Python3:urllib.error.HTTPError:HTTP错误403:禁止

请帮我!

我正在使用Python3.3和以下代码:

import urllib.request
import sys
Open_Page = urllib.request.urlopen(
        "http://wowcircle.com"
    ).read().decode().encode('utf-8')

我认为:

    Traceback (most recent call last):
  File "C:\Users\1\Desktop\WCLauncer\reg.py", line 5, in <module>
    "http://forum.wowcircle.com"
  File "C:\Python33\lib\urllib\request.py", line 156, in urlopen
    return opener.open(url, data, timeout)
  File "C:\Python33\lib\urllib\request.py", line 475, in open
    response = meth(req, response)
  File "C:\Python33\lib\urllib\request.py", line 587, in http_response
    'http', request, response, code, msg, hdrs)
  File "C:\Python33\lib\urllib\request.py", line 507, in error
    result = self._call_chain(*args)
  File "C:\Python33\lib\urllib\request.py", line 447, in _call_chain
    result = func(*args)
  File "C:\Python33\lib\urllib\request.py", line 692, in http_error_302
    return self.parent.open(new, timeout=req.timeout)
  File "C:\Python33\lib\urllib\request.py", line 475, in open
    response = meth(req, response)
  File "C:\Python33\lib\urllib\request.py", line 587, in http_response
    'http', request, response, code, msg, hdrs)
  File "C:\Python33\lib\urllib\request.py", line 507, in error
    result = self._call_chain(*args)
  File "C:\Python33\lib\urllib\request.py", line 447, in _call_chain
    result = func(*args)
  File "C:\Python33\lib\urllib\request.py", line 692, in http_error_302
    return self.parent.open(new, timeout=req.timeout)
  File "C:\Python33\lib\urllib\request.py", line 475, in open
    response = meth(req, response)
  File "C:\Python33\lib\urllib\request.py", line 587, in http_response
    'http', request, response, code, msg, hdrs)
  File "C:\Python33\lib\urllib\request.py", line 507, in error
    result = self._call_chain(*args)
  File "C:\Python33\lib\urllib\request.py", line 447, in _call_chain
    result = func(*args)
  File "C:\Python33\lib\urllib\request.py", line 692, in http_error_302
    return self.parent.open(new, timeout=req.timeout)
  File "C:\Python33\lib\urllib\request.py", line 475, in open
    response = meth(req, response)
  File "C:\Python33\lib\urllib\request.py", line 587, in http_response
    'http', request, response, code, msg, hdrs)
  File "C:\Python33\lib\urllib\request.py", line 513, in error
    return self._call_chain(*args)
  File "C:\Python33\lib\urllib\request.py", line 447, in _call_chain
    result = func(*args)
  File "C:\Python33\lib\urllib\request.py", line 595, in http_error_default
    raise HTTPError(req.full_url, code, msg, hdrs, fp)
urllib.error.HTTPError: HTTP Error 403: Forbidden

我了解,我无法访问wowcircle.com网站.但是我只想获取源代码!我相信我可以在没有麻烦的情况下做到这一点,但是又如何呢?

最佳答案
我建议您相应设置标题.看一下浏览器发送的内容(HTTP标头插件).

一个函数可能看起来像这样:

def openAsOpera(url):
    u = urllib.URLopener() # Python 3: urllib.request.URLOpener
    u.addheaders = []
    u.addheader('User-Agent', 'Opera/9.80 (Windows NT 6.1; WOW64; U; de) Presto/2.10.289 Version/12.01')
    u.addheader('Accept-Language', 'de-DE,de;q=0.9,en;q=0.8')
    u.addheader('Accept', 'text/html, application/xml;q=0.9, application/xhtml+xml, image/png, image/webp, image/jpeg, image/gif, image/x-xbitmap, */*;q=0.1')
    f = u.open(url)
    content = f.read()
    f.close()
    return content

这样可以避免某些网页上出现一些错误,这些错误会使客户端比基本版本期望更多.

现在我得到这个错误:

Traceback (most recent call last):
  File "<pyshell#0>", line 1, in <module>
    s = openAsOpera('http://wowcircle.com/')
  File "C:....pyw", line 522, in openAsOpera
    f = u.open(url)
  File "C:\Python27\lib\urllib.py", line 208, in open
    return getattr(self, name)(url)
  File "C:\Python27\lib\urllib.py", line 359, in open_http
    return self.http_error(url, fp, errcode, errmsg, headers)
  File "C:\Python27\lib\urllib.py", line 376, in http_error
    return self.http_error_default(url, fp, errcode, errmsg, headers)
  File "C:\Python27\lib\urllib.py", line 381, in http_error_default
    raise IOError, ('http error', errcode, errmsg, headers)
IOError: ('http error', 302, 'Moved Temporarily', <httplib.HTTPMessage instance at 0x02C8F1C0>)

这意味着您现在可以访问,因为您是在伪造真实浏览器的请求.

>>> try: s = openAsOpera('http://wowcircle.com/?pmtry=1')
except: import sys; ty, err, tb = sys.exc_info()

>>> err.args[3].headers
['Server: nginx\r\n', 'Date: Sat, 05 Apr 2014 07:42:00 GMT\r\n', 'Content-Type: text/html\r\n', 'Content-Length: 154\r\n', 'Connection: close\r\n', 'Set-Cookie: PMBC=9979187990a58a5bfdaa6d1380ad6156; path=/\r\n', 'Location: http://wowcircle.com/?pmtry=1\r\n']

一个想法在那里注意到:重定向转到此位置:http://wowcircle.com/?pmtry=1,然后转到:http://wowcircle.com/?pmtry=2.它计数.并似乎在等待cookie.

因此,我的分析结果是:别忘了每次访问站点时都发送cookie.

转载注明原文:python-3.x-Python3:urllib.error.HTTPError:HTTP错误403:禁止 - 代码日志