python登陆asp网站页面的实现代码
使用python来登录asp网站和登录其他网站差不多,只是因为asp页面在每次请求的时候都要带上viewstate,因此使用python来登录的话就多了一个步骤,获得这个页面的viewstate之后带上这个和你要post或get到该页面的请求数据就好了,下面这段程序是登录一个asp系统,然后搜索某些数据并将这些数据保存下来.
#coding=utf-8 importurllib2frombs4importBeautifulSoupimporturllibimportcookielibimportreimporthttplibimporttime loginUrl="登录地址" headers={"User-Agent":"Mozilla/5.0(WindowsNT6.1)AppleWebKit/537.36(KHTML,likeGecko)Chrome/37.0.2062.120Safari/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) """printloginPageHTML soup=BeautifulSoup(loginPageHTML) __VIEWSTATE=soup.find(id="__VIEWSTATE")['value'] __EVENTVALIDATION=soup.find(id="__EVENTVALIDATION")['value'] print__VIEWSTATEprint__EVENTVALIDATION login_data={ '__EVENTTARGET':'','__EVENTARGUMENT':'','__LASTFOCUS':'','__VIEWSTATE':__VIEWSTATE,'__EVENTVALIDATION':__EVENTVALIDATION,'ClienScreentHeight':'768','TextBoxUserID':'username','TextBoxPWD':'password','drpLanguage':'zh-CN','ButtonConfirm.x':'45','ButtonConfirm.y':'64' } loginHeader={ 'User-Agent':'sssssssssssssssssssssss' } loginData=urllib.urlencode(login_data) loginRequest=urllib2.Request(loginUrl,loginData,headers) loginResponse=pageOpener.open(loginRequest) printloginResponse theurl='登录后搜索页面地址' 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) printloginResponseprintsearchResponse 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()
原文:python登陆asp网站页面