自動登陸:http://home.51cto.com php
一、分析:html
使用httpfox抓取手動登陸home.51cto.com的過程,過程以下:python
點登陸,提交用戶名與密碼到http://home.51cto.com/index.php?s=/Index/doLogin 這個地址,正確後,他會返回的內容包含不少連接,如第二個圖片。而後分別get這些連接。請求完這些連接後,再訪問我的主頁http://home.51cto.com/index.php?s=/Home/index 服務器
二、思路有了,那麼就是寫代碼了。(登陸是使用onepc的賬號,完後後能夠返回的html中找到onepc)ide
登陸代碼參考網上的資料。post
import urllib.request import urllib.parse import http.cookiejar import re posturl='http://home.51cto.com/index.php?s=/Index/doLogin' url='http://home.51cto.com' cookie = http.cookiejar.LWPCookieJar() cookie_support = urllib.request.HTTPCookieProcessor(cookie) opener = urllib.request.build_opener(cookie_support, urllib.request.HTTPHandler) urllib.request.install_opener(opener) urllib.request.urlopen(url) headers = {'User-Agent':'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:30.0) Gecko/20100101 Firefox/30.0','Referer':'http://home.51cto.com/','Host':'home.51cto.com'} postdata={'email':'hxw168','passwd':'xxxx','reback':''} #密碼未加密 postdata=urllib.parse.urlencode(postdata).encode('utf-8') request=urllib.request.Request(posturl,postdata,headers) response=urllib.request.urlopen(request) html=response.read().decode('utf-8') #print(response.read().decode('utf-8')) #print(html) #這裏把post成功後返回的內容中取得各個url,而後分別執行。 r_geturl=re.compile('src="([^"]+)"',re.S) logurllist=r_geturl.findall(html) for l in logurllist: urllib.request.urlopen(l) s=urllib.request.urlopen('http://home.51cto.com/index.php?s=/Home/index') print(s.read().decode('utf-8')) #這裏能夠讀取到用戶賬號、短信息 登陸成功後就能夠作別的事了。