Search code examples
pythonlinuxmechanize

I want to obtain source of a site after authentication with python mechanize. HTTP Error 403: Forbidden


import mechanize
import http.cookiejar as cookielib

br = mechanize.Browser()

cj = cookielib.LWPCookieJar()
br.set_cookiejar(cj)

br.set_handle_equiv(True)
br.set_handle_gzip(True)
br.set_handle_redirect(True)
br.set_handle_referer(True)
br.set_handle_robots(False)

br.set_handle_refresh(mechanize._http.HTTPRefreshProcessor(), max_time=1)

br.addheaders = [('User-agent', 'Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.0.1) Gecko/2008071615 Fedora/3.0.1-1.fc9 Firefox/3.0.1')]

br.open('https://vortex.gg/account/signin')
br._factory.is_html = True
br.select_form(nr=0)
br.form['Username'] = '***************'
br.form['Password'] = '***************'
#i am not sure about this proxy
br.set_proxies({"http":'104.238.176.152:8080'})
r = br.submit()
html = r.read()
#i wanna see html code after autentification
f = open('source_vox.html', 'w')
f.write(html)
f.close

* Traceback (most recent call last): File "test_vox.py", line 19, in br.open('https://vortex.gg/account/signin') File "/usr/lib/python2.7/dist-packages/mechanize/_mechanize.py", line 253, in open return self._mech_open(url_or_request, data, timeout=timeout) File "/usr/lib/python2.7/dist-packages/mechanize/_mechanize.py", line 309, in _mech_open raise response mechanize._response.httperror_seek_wrapper: HTTP Error 403: Forbidden *

  • Is there a way to figure out this task? Would it work with request module? *

Solution

  • The page https://vortex.gg/account/signin cannot be accessed. Therefore that error. I think it is NOT a python error but simple the fact you do not enough authorizatio to access that page. Even if you copy and paste to the browser you get the same error.