Python3: urllib.error.HTTPError: HTTP Error 403: Forbidden

泄露秘密 提交于 2019-12-05 23:26:43

I advise you to set the headers accordingly. Have a look what your browser sends (HTTP headers plugin).

A function may look like this:

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

This gets you around some errors on some webpages which expect more from a client than the basic version does.

Now I get this error:

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>)

Which means that you get access now because you fake the request of a real browser.

>>> 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']

One thinkg to notice there: The redirect goes to this location: http://wowcircle.com/?pmtry=1 and then to whis: http://wowcircle.com/?pmtry=2. It counts up. And seems to wait for the cookie.

SO the result of my analysis is: Do not forget to send the cookie every time you access the site.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!