python模拟登录百度代码分享(获取百度贴吧等级)
时间:2022-05-13 02:54:34
# -*- coding: utf8 -*-
'''
Created on 2013-12-19
@author: good-temper
'''
import urllib2
import urllib
import cookielib
import re
import bs4
URL_BAIDU_INDEX = u'http://www.baidu.com/';
#https://passport.baidu.com/v2/api/?getapi&class=login&tpl=mn&tangram=true 也可以用这个
URL_BAIDU_TOKEN = 'https://passport.baidu.com/v2/api/?getapi&tpl=pp&apiver=v3&class=login';
URL_BAIDU_LOGIN = 'https://passport.baidu.com/v2/api/?login';
#设置用户名、密码
username = '';
password = '';
#设置cookie,这里cookiejar可自动管理,无需手动指定
cj = cookielib.CookieJar();
opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj));
urllib2.install_opener(opener);
reqReturn = urllib2.urlopen(URL_BAIDU_INDEX);
#获取token,
tokenReturn = urllib2.urlopen(URL_BAIDU_TOKEN);
matchVal = re.search(u'"token" : "(?P<tokenVal>.*?)"',tokenReturn.read());
tokenVal = matchVal.group('tokenVal');
#构造登录请求参数,该请求数据是通过抓包获得,对应https://passport.baidu.com/v2/api/?login请求
postData = {
'username' : username,
'password' : password,
'u' : 'https://passport.baidu.com/',
'tpl' : 'pp',
'token' : tokenVal,
'staticpage' : 'https://passport.baidu.com/static/passpc-account/html/v3Jump.html',
'isPhone' : 'false',
'charset' : 'UTF-8',
'callback' : 'parent.bd__pcbs__ra48vi'
};
postData = urllib.urlencode(postData);
#发送登录请求
loginRequest = urllib2.Request(URL_BAIDU_LOGIN,postData);
loginRequest.add_header('Accept','text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8');
loginRequest.add_header('Accept-Encoding','gzip,deflate,sdch');
loginRequest.add_header('Accept-Language','zh-CN,zh;q=0.8');
loginRequest.add_header('User-Agent','Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/28.0.1500.72 Safari/537.36');
loginRequest.add_header('Content-Type','application/x-www-form-urlencoded');
sendPost = urllib2.urlopen(loginRequest);
#查看贴吧个人主页 ,测试是否登陆成功,由于cookie自动管理,这里处理起来方便很多
#http://tieba.baidu.com/home/main?un=XXXX&fr=index 这个是贴吧个人主页,各项信息都可以在此找到链接
teibaUrl = 'http://tieba.baidu.com/f/like/mylike?v=1387441831248'
content = urllib2.urlopen(teibaUrl).read();
content = content.decode('gbk').encode('utf8');
print content;
#解析数据,用的BeautifulSoup4,感觉没有jsoup用的爽
soup = bs4.BeautifulSoup(content);
list = soup.findAll('tr');
list = list[1:len(list)];
careTeibalist = [];
print '贴吧链接\t吧名\t等级';
for elem in list:
soup1 = bs4.BeautifulSoup(str(elem));
print 'http://tieba.baidu.com/'+soup1.find('a')['href']+'\t'+soup1.find('a')['title']+'\t'+soup1.find('a',{'class','like_badge'})['title'];
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Python实现抓取腾讯视频所有电影的示例代码
![](https://img.aspxhome.com/file/2023/7/77657_0s.jpg)
MySQL Dump/Restore
Python内建类型int源码学习
![](https://img.aspxhome.com/file/2023/0/59150_0s.png)
Python3 sys.argv[ ]用法详解
![](https://img.aspxhome.com/file/2023/5/100865_0s.png)
Go语言实现AOI区域视野管理流程详解
![](https://img.aspxhome.com/file/2023/7/90477_0s.png)
Python实现去除列表中重复元素的方法总结【7种方法】
SQL查询效率:100w数据查询只需要1秒钟
关于python3安装pip及requests库的导入问题
![](https://img.aspxhome.com/file/2023/5/115015_0s.png)
Django实现简单网页弹出警告代码
python 含子图的gif生成时内存溢出的方法
pycharm配置Qt Designer工具的图文教程
![](https://img.aspxhome.com/file/2023/8/83928_0s.png)
Go语言反射reflect.Value实现方法的调用
css样式表实现首写字母大写
python实现将excel文件转化成CSV格式
Python如何生成随机高斯模糊图片详解
![](https://img.aspxhome.com/file/2023/1/107061_0s.png)
python RC4加密操作示例【测试可用】
![](https://img.aspxhome.com/file/2023/8/107658_0s.png)
菜鸟课堂:详述如何提高MySQL中数据装载效率
基于PyQt5完成pdf转word功能
![](https://img.aspxhome.com/file/2023/4/107874_0s.png)
python BlockingScheduler定时任务及其他方式的实现
Python使用getpass库读取密码的示例
![](https://img.aspxhome.com/file/2023/8/118498_0s.jpg)