0

For example there are 2 webpages that need to be opened in sequence:

mechanize Documentation shows there’s a way to open them one by one in the case that the 2nd requires a cookie that was set in the 1st. as follows:

import mechanize
import urllib

request = mechanize.Request("http://www.example.com/")
response = mechanize.urlopen(request)

# let's say this next request requires a cookie that was set in response

request2 = mechanize.Request("http://www.example.com/spam.html/")
response2 = mechanize.urlopen(request2)

on the other hand, ignoring the ‘params’ part below, is it functioning the same, especially for the cookie part? What’s the difference of these 2 sets of codes?

br = mechanize.Browser()
postDict = {'username' : 'name',
            'password' : 'pass',}

params = urllib.urlencode(postDict)
response = br.open(‘www.example.com’, params)
response1 = br.open(‘www.example.com/spam.html’)

thanks.

. . . . .

My question behind this question is, by using the 2nd set of code, it manages to login ‘http://www.example.com/’. but when trying to open ‘www.example.com/spam.html’, it returns the content of the 1st page, which is not wanted.

By using a real Brower, for example Internet Explorer, I can login ‘http://www.example.com/’ in one tag, and successfully open ‘www.example.com/spam.html’ in another tag in the same browser. No problem at all.

Why the code is not returning the 2nd page? Thanks.

ojs
  • 492
  • 4
  • 13
Mark K
  • 8,767
  • 14
  • 58
  • 118
  • Second code you are only initializing one browser which means you can already use the cookie on subsequent requests. – majidarif May 23 '14 at 07:05
  • see http://stackoverflow.com/questions/4720470/using-python-and-mechanize-to-submit-form-data-and-authenticate for a good example of how to handle cookies with mechanize. – ojs May 23 '14 at 10:38

0 Answers0