自动登录:http://home.51cto.com


1、分析:

使用httpfox抓取手动登录home.51cto.com的过程,过程如下:


点登录,提交用户名与密码到http://home.51cto.com/index.php?s=/Index/doLogin 这个地址,正确后,他会返回的内容包含很多链接,如第二个图片。然后分别get这些链接。请求完这些链接后,再访问个人主页http://home.51cto.com/index.php?s=/Home/index


wKioL1PfpGqitKm_AAjz59AuFY4626.jpg


post成功后服务器返回的内容。wKiom1PfpDvylFIHAAut3Cmz1iA052.jpg



2、思路有了,那么就是写代码了。(登录是使用onepc的帐号,完后后可以返回的html中找到onepc)


登录代码参考网上的资料。


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')) #这里可以读取到用户帐号、短信息

登录成功后就可以做别的事了。