Recently in the network operation of learning Python, there are a lot of analog landing methods on the Internet. Take a note here. It is easier to do without a verification code.
#coding: Utf-8import reimport urllibimport urllib2import cookielibimport osimport jsonimport sysimport timefrom urllib Import quote,unquotefrom urllib2 import httperrorfrom urllib2 import urlerror# login Module def login (): TRY:CJ = Cookie Lib. Cookiejar (); Opener = Urllib2.build_opener (urllib2. Httpcookieprocessor (CJ)); Urllib2.install_opener (opener); #打开获得 Cookie Info_url = "Http://portal.uestc.edu.cn/index.portal" Info_login_url = "https://uis.uestc.edu.cn /amserver/ui/login "; Infoopen = Urllib2.urlopen (info_url) Print 1 #生成要发送的数据 logindata = {' IDToken0 ': ', ' IDToken1 ': ' 20142106XXXX ', ' IDToken2 ': ' xxxxxx ', ' Idbutton ': ' Submit ', ' goto ': ' Ahr0cdo Vl3bvcnrhbc51zxn0yy5lzhuuy24vbg9naw4ucg9ydgfs ', ' encoded ': ' true ', ' gx_charset ': ' UTF-8 ',} ; Print 2 #把发送数据转换格式 logindata = Urllib.urlencode (logiNdata); #生成请求数据 info_login_request = urllib2. Request (Info_login_url, logindata); #添加数据头部 info_login_request.add_header (' Content-type ', "application/x-www-form-urlencoded"); Info_login_request.add_header (' user-agent ', "mozilla/5.0 (X11; Linux i686) applewebkit/537.36 (khtml, like Gecko) chrome/39.0.2171.95 safari/537.36 "); Print 3 #发送请求, try logging in Info_login_open = Urllib2.urlopen (info_login_request); Print 4 #验证登陆是否成功 TT = Urllib2.urlopen (info_url). Read () Fout=open ("Tt.html", "W") Fout.write (TT ) print ' success! ' Except:print "Landing Error! "If __name__ = =" __main__ ": Login ()
Tested successfully
IDtoken1 and IDtoken2 fill in the user name and password. Others do not change
Linux directly below the Python spider.py can be run
Python Simulation Login: Simulation login UESTC Information portal test