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.