|
登录没问题,关注有点问题,暂时还没找出来,总是返回空,不知道为什么,不过大概还是这样的,看看吧import requests as rs
import time
import re
import datetime
import random
import urllib.parse
class login:
def __init__(self,username,password):
self.username=username
self.password=password
self.se=rs.session()
self.data={}
self.data['ppui_logintime']=random.randint(10000,9999999)
self.data['charset']='utf-8'
self.data['codestring']=self.get_codestring()
self.data['token']=self.get_token()
self.data['isPhone']='false'
self.data['index']='0'
self.data['u']=''
self.data['safeflg']='0'
self.data['staticpage']='http%3A%2F%2Fwww.baidu.com%2Fcache%2Fuser%2Fhtml%2Fjump.html'
self.data['loginType']='1'
self.data['tpl']='mn'
self.data['callback']='parent.bdPass.api.login._postCallback'
self.data['username']=self.username
self.data['password']=self.password
self.data['verifycode']=self.get_verify()
self.data['mem_pass']='on'
self.head = {}
self.head['User-Agent'] = 'Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko' \
') Chrome/55.0.2883.87 Mobile Safari/537.36'
def get_parameter(self, re_str, html):
match = re.compile(re_str)
parameter = match.findall(html)[0]
return parameter
def get_codestring(self):
url='https://passport.baidu.com/v2/api/?logincheck&callback=bdPass.api.login._needCodestringCheckCallback&tpl=' \
'mn&charset=UTF-8&index=0&username='+self.username+'&isphone=false&time='+self.get_posttime()
html = self.se.get(url).text
# match = re.compile("codestring\":\"(.*?)\"")
# codestring = match.findall(html)[0]
# return codestring
codestring=self.get_parameter("codestring\":\"(.*?)\"",html)
return codestring
def get_token(self):
rs.get('http://news.baidu.com/')
url='https://passport.baidu.com/v2/api/?getapi&class=login&tpl=mn&tangram=true'
html=self.se.get(url).text
# match = re.compile("token='(.*?)'")
# token = match.findall(html)[0]
# return token
token=self.get_parameter("token='(.*?)'",html)
return token
def get_verify(self):
url='https://passport.baidu.com/cgi-bin/genimage?'+self.data['codestring']+'&v='+self.get_posttime()
html=self.se.get(url).content
with open(r'C:\Users\Mr-zhang\Desktop\pic.png','wb') as f:
f.write(html)
verify=input('请输入验证码')
return verify
def get_posttime(self):
now_time = str(int(time.mktime(datetime.datetime.now().timetuple())))
return now_time
def attenion(self):
tie_name = input('请输入贴吧名:')
reponse = self.se.get('http://tieba.baidu.com/f?ie=utf-8&kw='+str(tie_name), headers=self.head).text
tbs = self.get_parameter("\"tbs\":\"(.*?)\"", reponse)
fid = self.get_parameter("\"forum_id\":(.*?),", reponse)
data = {}
data['fid'] = fid
data['fname'] = tie_name
data['uid'] = self.username+'&ie=utf-8'
data['ie'] = 'gbk'
data['tbs'] = tbs
data=urllib.parse.urlencode(data).encode('utf-8')
print(data)
reponse = self.se.post('http://tieba.baidu.com/f/like/commit/add', data=data, headers=self.head).text
print(reponse)
def main(self):
post_url='https://passport.baidu.com/v2/api/?login'
response=self.se.post(post_url,data=self.data,headers=self.head).text
#print(response)
if 'error=0' in response:
print('登录成功')
self.attenion()
else:
print('登录失败')
if __name__ == '__main__':
s=login('账号','密码')
s.main()
|
|