import requestsimport base64import timeimport rsaimport binasciiimport urllib.parseimport re,urllibfrom bs4 import BeautifulSoupclass WeiBo(): def __init__(self): self.session = requests.session() self.headers = { ‘User-Agent‘:‘Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.108 Safari/537.36‘ } self.proxies = {} def get_secer_data(self, su): prelogin_url = ‘https://login.sina.com.cn/sso/prelogin.php?entry=weibo&callback=sinaSSOController.preloginCallBack&su=%s&rsakt=mod&client=ssologin.js(v1.4.19)&_=%s‘ % ( su, str(int(time.time() * 1000))) pre_data_res = self.session.get(prelogin_url, headers=self.headers, proxies=self.proxies) sever_data = eval(pre_data_res.content.decode("utf-8").replace("sinaSSOController.preloginCallBack", ‘‘)) return sever_data def get_su(self, username): username_quote = urllib.parse.quote_plus(username) username_base64 = base64.b64encode(username_quote.encode("utf-8")) return username_base64.decode("utf-8") def get_password(self, password, servertime, nonce, pubkey): rsaPublickey = int(pubkey, 16) key = rsa.PublicKey(rsaPublickey, 65537) message = str(servertime) + ‘\t‘ + str(nonce) + ‘\n‘ + str(password) message = message.encode("utf-8") passwor = rsa.encrypt(message, key) passwor = binascii.b2a_hex(passwor) return passwor def login(self, su, sp, sever_data, nonce, rsakv, servertime): data = { ‘entry‘: ‘weibo‘, ‘gateway‘: ‘1‘, ‘from‘: ‘‘, ‘savestate‘: ‘7‘, ‘qrcode_flag‘: ‘false‘, ‘useticket‘: ‘1‘, ‘pagerefer‘: ‘https://passport.weibo.com/visitor/visitor?entry=miniblog&a=enter&url=https%3A%2F%2Fweibo.com%2F%3Fsudaref%3Dwww.baidu.com%26display%3D0%26retcode%3D6102&domain=.weibo.com&sudaref=https%3A%2F%2Flogin.sina.com.cn%2Fsso%2Flogin.php%3Furl%3Dhttps%253A%252F%252Fweibo.com%252F%26_rand%3D1576318124.3653%26gateway%3D1%26service%3Dminiblog%26entry%3Dminiblog%26useticket%3D1%26returntype%3DMETA%26sudaref%3Dhttps%253A%252F%252Fwww.baidu.com%252Flink%253Furl%253D1Gxs_A1v8Lqn_zffSX5I3EMIqUQTJ3CoF4M7JWEeKni%2526wd%253D%2526eqid%253D936d55f70000e6ff000000045df4b4a9%26_client_version%3D0.6.28&ua=php-sso_sdk_client-0.6.28&_rand=1576318124.7105‘, ‘vsnf‘: ‘1‘, ‘su‘: su, ‘service‘: ‘miniblog‘, ‘servertime‘: servertime, ‘nonce‘: nonce, ‘pwencode‘: ‘rsa2‘, ‘rsakv‘: rsakv, ‘sp‘: sp, ‘sr‘: ‘1366 * 768‘, ‘encoding‘: ‘UTF-8‘, ‘prelt‘: ‘35‘, ‘url‘: ‘https://weibo.com/ajaxlogin.php?framelogin=1&callback=parent.sinaSSOController.feedBackUrlCallBack‘, ‘returntype‘: ‘META‘ } url = ‘https://login.sina.com.cn/sso/login.php?client=ssologin.js(v1.4.19)‘ login_page = self.session.post(url, data=data) login_loop = (login_page.content.decode("GBK")) pa = r‘location\.replace\([\‘"](.*?)[\‘"]\)‘ loop_url = re.findall(pa, login_loop)[0] lodin_index = self.session.get(loop_url) url1 = re.findall(pa, lodin_index.text)[0] url = ‘https://weibo.com/ajaxlogin.php?‘ html1 = self.session.get(url1) uuid_pa = r‘"uniqueid":"(.*?)"‘ html = self.session.get(url, headers=self.headers).text uuid_res = re.findall(uuid_pa,html,re.S)[0] return uuid_res def get_uid(self,name): url = ‘https://s.weibo.com/user?q=%s‘%name html = self.session.get(url,headers = self.headers).text soup = BeautifulSoup(html,‘lxml‘) p1 = soup.find(name = ‘a‘,attrs={‘class‘:‘name‘}) x = p1.attrs[‘href‘] pa = re.compile(‘\d+‘) uid = re.findall(pa,x)[0] time.sleep(0.5) return uid def fwb(self,username,password): su = self.get_su(username) sever_data = self.get_secer_data(su) servertime = sever_data["servertime"] nonce = sever_data["nonce"] rsakv = sever_data["rsakv"] pubkey = sever_data["pubkey"] sp = self.get_password(password, servertime, nonce, pubkey) uuid = self.login(su, sp, sever_data, nonce, rsakv, servertime) text = input("请输入你想要的发送的文字:") data = { ‘location‘: ‘v6_content_home‘, ‘text‘: ‘text‘, ‘appkey‘:‘‘, ‘style_type‘: ‘1‘, ‘pic_id‘:‘‘, ‘tid‘:‘‘, ‘pdetail‘:‘‘, ‘mid‘:‘‘, ‘isReEdit‘: ‘false‘, ‘rank‘: 0, ‘rankid‘:‘‘, ‘module‘: ‘stissue‘, ‘pub_source‘: ‘main_‘, ‘pub_type‘: ‘dialog‘, ‘isPri‘: 0, ‘_t‘: 0 } headers = { ‘Referer‘:‘https://weibo.com/u/%s‘%uuid+‘/home?topnav=1&wvr=6‘, ‘User-Agent‘:‘Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.108 Safari/537.36‘ } url2 = ‘https://weibo.com/u/%s‘%uuid+‘/home?topnav=1&wvr=6‘ time.sleep(0.5) html = self.session.get(url2,headers = headers) time.sleep(0.5) url = ‘https://weibo.com/aj/mblog/add?ajwvr=6&__rnd=%s‘ %(int(time.time() * 1000)) fasong = self.session.post(url,data = data,headers = self.headers) print(url) print(fasong.status_code) ‘‘‘ print("成功了哦") else: print("失败了")‘‘‘ def guanzhu(self,username,password): su = self.get_su(username) sever_data = self.get_secer_data(su) servertime = sever_data["servertime"] nonce = sever_data["nonce"] rsakv = sever_data["rsakv"] pubkey = sever_data["pubkey"] sp = self.get_password(password, servertime, nonce, pubkey) self.login(su, sp, sever_data, nonce, rsakv, servertime) headerss = { ‘Referer‘: ‘https://weibo.com/u/5272436964?is_hot=1‘, ‘User-Agent‘: ‘Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.108 Safari/537.36‘ } name = input("请输入想要关注的人:") uid = self.get_uid(name) url1 = ‘https://weibo.com/u/%s‘%uid html1 = self.session.get(url1,headers = self.headers) dat = { ‘uid‘: uid, ‘objectid‘:‘‘, ‘f‘: ‘1‘, ‘extra‘:‘‘, ‘refer_sort‘:‘‘, ‘refer_flag‘: ‘1005050001‘, ‘location‘: ‘page_100505_home‘, ‘oid‘: uid, ‘wforce‘: ‘1‘, ‘nogroup‘: ‘1‘, ‘fnick‘: name, ‘refer_lflag‘:‘‘, ‘refer_from‘: ‘profile_headerv6‘, ‘template‘: ‘7‘, ‘special_focus‘: ‘1‘, ‘isrecommend‘: ‘1‘, ‘is_special‘: ‘0‘, } url3 = ‘https://weibo.com/aj/f/followed?ajwvr=6&__rnd=%s‘%(int(time.time()*1000)) htmls = self.session.post(url3,data=dat,headers = headerss) print(htmls.text) if htmls.status_code ==200: print(‘true‘) def shiyan(self,username,password): su = self.get_su(username) sever_data = self.get_secer_data(su) servertime = sever_data["servertime"] nonce = sever_data["nonce"] rsakv = sever_data["rsakv"] pubkey = sever_data["pubkey"] sp = self.get_password(password, servertime, nonce, pubkey) uuid = self.login(su, sp, sever_data, nonce, rsakv, servertime)微博模拟登陆后发文字微博以及自动关注用户 -BY Huyer ——2019 12 21
原文地址:https://www.cnblogs.com/huyer/p/12078628.html
时间: 2024-10-12 23:19:22