使用python来登录asp网站和登录其他网站差不多,只是因为asp页面在每次请求的时候都要带上viewstate,因此使用python来登录的话就多了一个步骤,获得这个页面的viewstate之后带上这个和你要post或get到该页面的请求数据就好了,下面这段程序是登录一个asp系统,然后搜索某些数据并将这些数据保存下来.
#coding=utf-8
import urllib2 from bs4 import beautifulsoup import urllib import cookielib import re import httplib import time
loginurl=”登录地址”
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()[‘value’]
__eventvalidation=soup.find()[‘value’]
print __viewstate print __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)
print loginresponse
theurl=’登录后搜索页面地址’
mainpagerequest = urllib2.request(theurl)
mainpagehtml = pageopener.open(mainpagerequest).read()
soup=beautifulsoup(mainpagehtml)
__viewstate=soup.find()[‘value’] #__eventvalidation=soup.find()[‘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()
原文:python登陆asp网站页面