Using Python to log on to an ASP site is about as much as logging on to another site, just because the ASP page takes a viewstate on every request, so using Python to log in is one more step, After you get the ViewState of this page and then bring this and the request data you want to post or get to the page, the following procedure is to log in to an ASP system and then search for some data and save the data.
#coding =utf-8 import urllib2 from BS4 import beautifulsoup import urllib import cookielib import re import httplib Impor T time loginurl= "login Address" headers={"user-agent": "mozilla/5.0 (Windows NT 6.1) applewebkit/537.36 (khtml, like Gecko) Chr ome/37.0.2062.120 safari/537.36 "} Studentcookie = Cookielib. Cookiejar () Pageopener = Urllib2.build_opener (urllib2. Httpcookieprocessor (Studentcookie)) Loginpagerequest = Urllib2. Request (loginurl) loginpagehtml = Pageopener.open (loginpagerequest). Read () "" "S=requests. Session () S.headers.update (headers) r=s.get (loginurl) "" "Print loginpagehtml Soup=beautifulsoup (loginpagehtml) __V Iewstate=soup.find (id= "__viewstate") [' Value '] __eventvalidation=soup.find (id= "__eventvalidation") [' Value '] Print __viewstate print __eventvalidation login_data={' __eventtarget ': ', ' __eventargument ': ', ' __lastfocus ': ', ' __VI Ewstate ': __viewstate, ' __eventvalidation ': __eventvalidation, ' clienscreentheight ': ' 768 ', ' textboxuserid ': ' Username ', ' TextBoxpwd ': ' Password ', ' drplanguage ': ' ZH-CN ', ' buttonconfirm.x ': ' The ', ' buttonconfirm.y ': ' Loginheader ' ' User-agent ': ' Sssssssssssssssssssssss '} logindata=urllib.urlencode (login_data) loginrequest = Urllib2. Request (loginurl, Logindata, headers) Loginresponse = Pageopener.open (loginrequest) print loginresponse theurl= ' login After the search page address ' mainpagerequest = urllib2. Request (theurl) mainpagehtml = Pageopener.open (mainpagerequest). Read () Soup=beautifulsoup (mainpagehtml) __VIEWSTAT E=soup.find (id= "__viewstate") [' value '] #__EVENTVALIDATION =soup.find (id= "__eventvalidation") [' Value '] print __ VIEWSTATE #print __eventvalidation searchdata={' __viewstate ': __viewstate, ' __eventvalidation ': ', ' Txtcopno ': ', ' txtcar_no_s ': ', ' drpstatus ': ', ' txthiddenoronline ': ' None ', ' txtauto_id ': ', ' drptype ': ', ' Drpbasetype ': ', ' Ddlisstatus ': 0, ' txticcard ': ', ' txtbill_no ': ', ' txtGDateTime1 ': ', ' txtGDateTime2 ': ', ' Drpfromka ': ', ' drptOKA ': ', ' btnsearch ': '%e6%9f%a5+%e8%af%a2%28f%29 '} data2=urllib.urlencode (Searchdata) Searchdata=urllib.urle Ncode (Searchdata) searcgrequest=urllib2. Request (theURL, Searchdata, headers) searchresponse=pageopener.open (searcgrequest) Print loginresponse print Searchre Sponse Searchhtml=searchresponse.read () filename= R ' C:\Users\Dell\Desktop\getlogin\file ' +time.strftime ('%d%h%m ', t Ime.localtime (Time.time ())) + '. html ' file=open (filename, ' W ') File.write (searchhtml) file.close () print ' End ' #raw_ Input ()
Original: Python Login ASP Site page