import requestsimport refrom PIL import Imageimport urllib3from bs4 import BeautifulSoupurllib3.disable_warnings() #不看警告 ‘‘‘扫描以后返回的地址https://wx2.qq.com/cgi-bin/mmwebwx-bin/[email protected]_0&uuid=gc_tSTRpaA==&lang=zh_CN&scan=1520997537https://wx2.qq.com/cgi-bin/mmwebwx-bin/[email protected]_0&uuid=gc_tSTRpaA==&lang=zh_CN&scan=1520997537‘‘‘class WxBott(object): def __init__(self): self.session = requests.session() #登陆问题用session self.session.verify = False #统一取消验证 def get_uuid(self): url = "https://login.wx.qq.com/jslogin?appid=wx782c26e4c19acffb&redirect_uri=https%3A%2F%2Fwx.qq.com%2Fcgi-bin%2Fmmwebwx-bin%2Fwebwxnewloginpage&fun=new&lang=zh_CN&_=1520997435513" #这个URL是由二维码的 headers 有一个参数不知道哪里来, 然后复制它, 在url里面找, 找到之后把有这个参数的url复制出来就是这个 result = self.session.get(url).text #获取到以后 在进行正则匹配, 匹配其中的uuid self.uuid = re.findall(r‘uuid = "(.*?)"‘,result)[0] #每次出来的uuid 都是不相同的 print(self.uuid) def get_qcode(self): ‘‘‘ 获取登陆二维码 ‘‘‘ # url = ‘https://login.weixin.qq.com/qrcode/{}‘.format(self.uuid) # with open(‘qqqqqcode.jpg‘,‘wb‘) as f: # # f.write(self.session.get(url).content) #content的意思是二进制 循环打开二进制图片文件 # image = Image.open(‘qcode.jpg‘) #读取图片对象 # image.show() url = ‘https://login.weixin.qq.com/qrcode/{}‘.format(self.uuid) with open(‘qqqqqcode.jpg‘,‘wb‘) as f: f.write(self.session.get(url).content) image = Image.open(‘qqqqqcode.jpg‘) #读取二进制图片 image.show() def visit_login(self): url = ‘https://login.wx.qq.com/cgi-bin/mmwebwx-bin/login?loginicon=true&uuid={}&tip=0&r=-591889347&_=1521010308313‘.format(self.uuid) #这个网址的由来是 复制了返回正真微信url的网址 while True: result = self.session.get(url).text print(result) if ‘200‘ in result: #如果包含200就跳出 self.redirect_url = re.findall(r‘redirect_uri="(.*?)";‘,result)[0] #这里的redirect_uri="(.*?)"; 很精髓 只是把需要的东西这样写(.*?) 别的不能动 后面加上[0] break print("登陆成功") #打开跳转的网址 print(self.redirect_url) def visit_parse(self): result = self.session.get(self.redirect_url,allow_redirects = False).text # allow_redirects = False 防止跳转 soup = BeautifulSoup(result,‘lxml‘) self.skey = soup.find(‘skey‘).text self.wxsid = soup.find(‘wxsid‘).text self.wxuin = soup.find(‘wxuin‘).text self.pass_ticket = soup.find(‘pass_ticket‘).text self.isgrayscale = soup.find(‘isgrayscale‘).text def visit_init(self): url = ‘https://wx2.qq.com/cgi-bin/mmwebwx-bin/webwxinit?r=-1033474349‘ data = {"BaseRequest":{"Uin":self.wxuin,"Sid":self.wxsid,"Skey":self.skey,"DeviceID":"e041800958560706"}} result = self.session.post(url,data)if __name__ == ‘__main__‘: wx = WxBott() wx.get_uuid() wx.get_qcode() wx.visit_login() wx.visit_parse() wx.visit_init() print(wx.skey) #print(wx.redirect_url)
原文地址:https://www.cnblogs.com/yuanjia8888/p/10124720.html
时间: 2024-10-10 05:47:13