【python】自动登陆51cto家园


自动登陆: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 服务器


wKioL1PfpGqitKm_AAjz59AuFY4626.jpg


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



二、思路有了,那么就是写代码了。(登陆是使用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')) #这里能够读取到用户账号、短信息

登陆成功后就能够作别的事了。
相关文章
相关标签/搜索