Implementation code for logging on to the asp website page using python

Source: Internet
Author: User
This article mainly introduces the implementation code for logging on to the asp website page using python. For more information about how to log on to the asp website using python, see, the reason is that the asp page requires viewstate to be included in each request. Therefore, if you use python to log on, there 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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.