Python模拟登录百度贴吧
2014-02-04 19:47
477 查看
贴上代码,,,基本都有注释,,,
总结下,其实没必要这么麻烦,,直接由浏览器导出cookie就能实现模拟登录了=。=
登录成功后会返回下面这个东西。。。
总结下,其实没必要这么麻烦,,直接由浏览器导出cookie就能实现模拟登录了=。=
# -*- coding=utf-8 -*- import urllib; import urllib.request; import json; import http; import http.cookiejar; import re; import os; from urllib.parse import urlencode; TOKEN_URL = "https://passport.baidu.com/v2/api/?getapi&tpl=mn&apiver=v3&class=login&callback=parent.bd__pcbs__hl4e6h"; INDEX_URL = "http://www.baidu.com/"; LOGIN_URL = "https://passport.baidu.com/v2/api/?login"; reg_token = re.compile("\"token\"\s+:\s+\"(\w+)\""); bdHeaders = { "Accept":"text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8", "Accept-Encoding":"gzip,deflate,sdch", "Accept-Language":"zh-CN,zh;q=0.8", "Host":"passport.baidu.com", "Origin":"http://www.baidu.com", "Referer":"http://www.baidu.com/", "User-Agent":"Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/29.0.1547.66 Safari/537.36 LBBROWSER", }; bdData = { "charset":"UTF-8", "token":"", "tpl":"mn", #重要,需要跟TOKEN_URL中的相同 "u":"http://tieba.baidu.com/", "username":"", "password":"", "callback":"parent.bd__pcbs__hl4e6h", #重要,需要跟TOKEN_URL中的相同 }; class tbLogin: def __init__(self): self._cookie = http.cookiejar.LWPCookieJar(); self._opener = urllib.request.build_opener(urllib.request.HTTPCookieProcessor(self._cookie)); def login(self, user = "", psw = ""): if(not os.path.exists("baidu.cookie")): self._initial(); self._getToken(); #取得token,必要 bdData["username"] = user; bdData["password"] = psw; bdData["token"] = self._token; request = urllib.request.Request(LOGIN_URL, headers = bdHeaders); self._opener.open(request, urlencode(bdData).encode("utf-8")); #登录 self._cookie.save("baidu.cookie", True, True); #保存cookie else: self._cookie.load("baidu.cookie", True, True); #加载cookie result = json.loads(self._opener.open("http://tieba.baidu.com/f/user/json_userinfo").read().decode("utf-8")); print(self._opener.open("http://tieba.baidu.com/f/user/json_userinfo").read().decode("utf-8")); if(result["no"] == 0): #判断是否登录成功 return self._opener; else: return None; def _getToken(self): self._token = reg_token.findall(self._opener.open(TOKEN_URL).read().decode("gbk"))[0]; def _initial(self): self._opener.open(INDEX_URL); def main(): robot = tbLogin(); #传入用户名和密码 opener = robot.login("", ""); if __name__ == "__main__": main();
登录成功后会返回下面这个东西。。。
相关文章推荐
- python3模拟百度登录并实现百度贴吧签到示例分享(百度贴吧自动签到)
- Python模拟登录wap版百度贴吧+自动回贴
- Python模拟登录wap版百度贴吧+自己主动回贴
- Python模拟登录wap版百度贴吧+自己主动回贴
- python模拟登录百度贴吧(百度贴吧登录)实例
- python模拟登录百度贴吧(百度贴吧登录)实例
- python模拟登录百度代码分享(获取百度贴吧等级)
- python模拟登录百度代码分享(获取百度贴吧等级)
- python3模拟百度登录并实现百度贴吧签到示例分享(百度贴吧自动签到)
- python模拟登录知乎
- 【原创】python模拟腾讯网页登录
- python -- 拉勾网爬虫模拟登录
- python第一周作业--------模拟登录
- Python爬虫数据存储MySQL【2】模拟登录网页
- python模拟登录URP教务系统评教
- python笔记 :模拟登录、三级菜单、购物车作业
- Python selenium自动化识别验证码模拟登录操作(二)
- python模拟登录新浪微博
- python 实现模拟登录网站并实现自动回复帖子
- python 模拟ef登录过程