Hi guys,
I tried to figure out how to deal with the cryptic "__VIEWSTATE" and Cie when you try to request a (POST) page with Python! This is the main source of many problems in my scenarios ... and I understand that when I was looking for answers / solutions, you do not have so many solutions (almost 0!).
In this Section:
Unable to load an ASP.NET page using Python urllib2
You can see that I give my solution with a state in analyzing the values of the critical fields every time the page loads ... This works, but it's pretty dumb :-)
for result in the_page.findAll('input', attrs={'name' : '__VIEWSTATE'}):
view_state = result['value']
for result_1 in the_page.findAll('input', attrs={'name' : '__EVENTVALIDATION'}):
event_validation = result_1['value']
for result_2 in the_page.findAll('input', attrs={'name' : '__PREVIOUSPAGE'}):
previous_page = result_2['value']
for result in the_page.findAll('input', attrs={'name' : '__EVENTTARGET'}):
event_target = result['value']
And after:
url = 'http://bandscore.ielts.org/search.aspx'
values = {
'__EVENTTARGET' : 'gdvSearchResults',
'__EVENTARGUMENT' : page,
'__VIEWSTATE' : view_state,
'__PREVIOUSPAGE' : previous_page,
'__EVENTVALIDATION' : event_validation,
'DropDownList1' : Country,
}
user_agent = 'Mozilla/5 (Solaris 10) Gecko'
headers = { 'User-Agent' : user_agent }
data = urllib.urlencode(values)
req = urllib2.Request(url, data, headers)
response = urllib2.urlopen(req)
thePage = response.read()
the_page = soup(thePage)
So here are some links with good explanations / some offer solutions:
What holds __VIEWSTATE?
http://aspalliance.com/articleViewer.aspx?aId=135&pId=
http://msdn.microsoft.com/en-us/library/system.web.ui.losformatter.aspx
http://weblogs.asp.net/infinitiesloop/archive/2006/08/03/Truly-Understanding-Viewstate.aspx
http://msdn.microsoft.com/en-us/library/ms972976.aspx
?
ASP.NET Python urllib2
, , , ; -)
EDIT1: , http://code.google.com/p/peekviewstate/source/browse/trunk/src/peekviewstate_example.py
(, / ... n00b, )