生活随笔
收集整理的這篇文章主要介紹了
【python】自动登录51cto家园
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
自動(dòng)登錄:http://home.51cto.com
1、分析:
使用httpfox抓取手動(dòng)登錄home.51cto.com的過程,過程如下:
點(diǎn)登錄,提交用戶名與密碼到http://home.51cto.com/index.php?s=/Index/doLogin?這個(gè)地址,正確后,他會(huì)返回的內(nèi)容包含很多鏈接,如第二個(gè)圖片。然后分別get這些鏈接。請(qǐng)求完這些鏈接后,再訪問個(gè)人主頁http://home.51cto.com/index.php?s=/Home/index
post成功后服務(wù)器返回的內(nèi)容。
2、思路有了,那么就是寫代碼了。(登錄是使用onepc的帳號(hào),完后后可以返回的html中找到onepc)
登錄代碼參考網(wǎng)上的資料。
import?urllib.request
import?urllib.parse
import?http.cookiejar
import?reposturl='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成功后返回的內(nèi)容中取得各個(gè)url,然后分別執(zhí)行。
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'))?#這里可以讀取到用戶帳號(hào)、短信息登錄成功后就可以做別的事了。
轉(zhuǎn)載于:https://blog.51cto.com/hxw168/1535751
總結(jié)
以上是生活随笔為你收集整理的【python】自动登录51cto家园的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。