欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

python模拟登录百度代码分享(获取百度贴吧等级)

程序员文章站 2022-06-20 21:07:37
复制代码 代码如下:# -*- coding: utf8 -*-'''created on 2013-12-19 @author: good-temper''' imp...

复制代码 代码如下:

# -*- 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'];