Python login asp Web page implementation code, pythonasp
Using python to log on to the asp Website is similar to logging on to other websites, but the asp page requires viewstate every time it is requested. Therefore, using python to log on is an additional step, after obtaining the viewstate of the page, you can bring the request data that you want to post or get to the page. The following program is used to log on to an asp system, 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 import time loginUrl = "Logon address" headers = {"User-Agent ": "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/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) _ VIEWSTATE = soup. find (id = "_ VIEWSTATE") ['value'] _ EVENTVALIDATION = soup. find (id = "_ EVENTVALIDATION") ['value'] print _ VIEWSTATE print _ EVENTVALIDATION login_data = {'_ EVENTTARGET ':'', '_ EVENTARGUMENT': '', '_ LASTFOCUS':'', '_ VIEWSTATE' :__ VIEWSTATE, '_ EVENTVALIDATION' :__ EVENTVALIDATION, 'clienscreentheight ': '123', 'textboxuserid': 'username', 'textboxpwd': 'Password', 'drplanguage': 'zh-cn', 'buttonconfirm. x': '45', 'buttonconfirm. y': '64'} loginHeader = {'user-agent': 'sssssssssssssssssssssssss'} loginData = urllib. urlencode (login_data) loginRequest = urllib2.Request (loginUrl, loginData, headers) loginResponse = pageOpener. open (loginRequest) print loginResponse theurl = 'search for the page address after logon 'mainpagerequest = urllib2.Request (theurl) mainPageHTML = pageOpener. open (mainPageRequest ). read () soup = BeautifulSoup (mainPageHTML) _ VIEWSTATE = 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. urlencode (searchdata) searcgRequest = urllib2.Request (theurl, searchData, headers) searchResponse = pageOpener. open (searcgRequest) print loginResponse print searchResponse searchHtml = searchResponse. read () filename = r 'C: \ Users \ Dell \ Desktop \ getlogin \ file'{time.strftime('{d}h}m', time.localtime(time.time({{{}'.html 'file = open (filename, 'w') file. write (searchHtml) file. close () print 'end' # raw_input ()
Original article: python login asp Website page