Sama Azari
Sama Azari

Reputation: 489

fetch and save http response before redirect using mechanize

I am trying to fetch a sample page in python

import mechanize
 def viewpage(url):
  browser = mechanize.Browser()
  page = browser.open(url)
  source_code = page.read()
  print source_code
viewpage('https://sama.com/index.php?req=1')

However everytime it will get redirected to index2.php (by a location header from webserver) thus for example the code print the response from index2.php rather than index.php is there anyway to avoid that?

Upvotes: 2

Views: 415

Answers (1)

RattleyCooper
RattleyCooper

Reputation: 5207

You can use urllib2 or requests for more complex stuff.

import urllib2

response = urllib2.urlopen("http://google.com")
page_source = response.read()

urllib2 is a built-in module and requests is 3rd party.

Upvotes: 1

Related Questions