python

关注公众号 jb51net

关闭
首页 > 脚本专栏 > python > python登陆asp网站

python登陆asp网站页面的实现代码

投稿:mdxy-dxy

这篇文章主要介绍了python登陆asp网站页面的实现代码,需要的朋友可以参考下

使用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(id="__VIEWSTATE")['value'] 
__EVENTVALIDATION=soup.find(id="__EVENTVALIDATION")['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(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()

原文:python登陆asp网站页面

您可能感兴趣的文章:
阅读全文