Last active
March 27, 2017 06:12
-
-
Save yeyuguo/18ab6e623ec08b0a6c713ad1b4c37076 to your computer and use it in GitHub Desktop.
python 模拟百度登陆
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # -*- coding: utf8 -*- | |
| ''' | |
| Created on 2013-12-19 | |
| @author: good-temper | |
| https://www.oschina.net/code/snippet_1183145_27432 | |
| ''' | |
| 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']; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # -*- 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'); | |
| print 'token:%s'%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; | |
| # 我自己的送花 | |
| # 目前验证是 不正确,代码获取到的token,不能使用到 点击花的api上; | |
| import requests | |
| if(tokenVal is None): | |
| tokenVal = 'cb0298c0858e8a73e837406175997829' | |
| # 以前记录的是 http,现在的是 https的 | |
| flowerUrl = 'https://baike.baidu.com/operation/api/starflowervote?lemmaid=6623568&tk=%s'%(tokenVal) | |
| testData = requests.get(flowerUrl); | |
| print '返回的信息:%s'%testData.text | |
| flowerCont = urllib2.urlopen(teibaUrl).read(); | |
| # flowerCont = flowerCont.decode('gbk').encode('utf8'); | |
| print flowerCont; | |
| # 我自己的送花 | |
| #解析数据,用的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']; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 鲜花请求url:GET请求 | |
| 20160728 | |
| 朴河宣: | |
| http://baike.baidu.com/operation/api/starflowervote?lemmaid=6623568&tk=93dae1759b9d53e1acbd40bff127bff5 | |
| http://baike.baidu.com/operation/api/starflowervote?lemmaid=6623568&tk=b64c58b985ebeadbc56ca19ee804e5e7 (第二天请求的) | |
| http://baike.baidu.com/operation/api/starflowervote?lemmaid=6623568&tk=b0a39405364a51871c7dd5fbe1d38550 (第三天) | |
| 郑秀晶 | |
| http://baike.baidu.com/operation/api/starflowervote?lemmaid=3136367&tk=93dae1759b9d53e1acbd40bff127bff5 | |
| curl命令测试: | |
| -X/--request 指定什么命令 | |
| curl -u 账户:密码 -i -X GET http://baike.baidu.com/operation/api/starflowervote?lemmaid=6623568&tk=93dae1759b9d53e1acbd40bff127bff5 | |
| 用赵兄弟的号 | |
| 朴河宣: | |
| http://baike.baidu.com/operation/api/starflowervote?lemmaid=6623568&tk=54cdcbb6b4d435f3f7f8bc73749078bc | |
| (浏览器访问已经是正确) | |
| http://baike.baidu.com/operation/api/starflowervote?lemmaid=6623568&tk=54cdcbb6b4d435f3f7f8bc73749078bc(第二天请求的) | |
| http://baike.baidu.com/operation/api/starflowervote?lemmaid=6623568&tk=60ef259b457236a33d152f4f9d3bc4e8 (第三天) | |
| 郑秀晶 | |
| http://baike.baidu.com/operation/api/starflowervote?lemmaid=3136367&tk=54cdcbb6b4d435f3f7f8bc73749078bc | |
| curl -u shengxiang999:890517abcd -i -X GET http://baike.baidu.com/operation/api/starflowervote?lemmaid=6623568&tk=54cdcbb6b4d435f3f7f8bc73749078bc | |
| curl -u 54cdcbb6b4d435f3f7f8bc73749078bc:unused -i -X GET http://baike.baidu.com/operation/api/starflowervote?lemmaid=6623568 | |
| 百度登录账户的url: | |
| https://passport.baidu.com/v2/?login | |
| 点送花请求的header: | |
| GET /operation/api/starflowervote?lemmaid=6623568&tk=0d5ec08ebe0c3627d59336d09742d2ac HTTP/1.1 | |
| Host: baike.baidu.com | |
| Connection: keep-alive | |
| Accept: application/json, text/javascript, */*; q=0.01 | |
| X-Requested-With: XMLHttpRequest | |
| User-Agent: Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/45.0.2454.99 Safari/537.36 | |
| Referer: http://baike.baidu.com/link?url=ZC5TfzZ7j-Wf8YwbK51Y9-qDOcocyZb_cCpuseMPAw0iE9j9KLRjHz0uHLZjVsceAGWRC1AeZT_ni4opvhn3Bq | |
| Accept-Encoding: gzip, deflate, sdch | |
| Accept-Language: zh-CN,zh;q=0.8 | |
| Cookie: BAIDUID=8445B9D0B48B5BFF2E2D709ABBC8F917:FG=1; BIDUPSID=8445B9D0B48B5BFF2E2D709ABBC8F917; PSTM=1466734150; __cfduid=d1208f160ebc6bd248fa7603ff44563831469717783; pgv_pvi=25581568; pgv_si=s5675312128; BDRCVFR[FhauBQh29_R]=mbxnW11j9Dfmh7GuZR8mvqV; BDUSS=jZ6bDRuVTJ0dmNsbks2VWstTEM5MWVYT01UMmw4cFMtOVkwYVpLa3BIM2UtOEpYQVFBQUFBJCQAAAAAAAAAAAEAAAB0imkrc2hlbmd4aWFuZzk5OQAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAN5um1febptXN; BDRCVFR[feWj1Vr5u3D]=I67x6TjHwwYf0; H_PS_PSSID=20693_1464_17943_20416_15096_12319_20745 | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment