杏璃之夏日物语,告白是从爱情开始的,yx007小游戏
@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'];
如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复
python求numpy中array按列非零元素的平均值案例
网友评论