1.登錄頁面需要有二維碼:
根據在network中查找,找到和他相關的內容
如果想要獲取那些數據就要訪問這個url
此url需要如何拼接,
登錄渲染出二維碼的flask代碼
#encoding=utf-8 from flask import Flask,request,render_template,request,session,jsonify import time import re import requests app=Flask(__name__) app.debug=True app.secret_key='aa' @app.route('/login',methods=["GET","POST"]) def login(): if request.method=="GET": ctime= time.time() a=str(int(ctime*1000)) #微信登錄頁面是 #獲取url 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&_={0}".format(a) # print('*'*50,url) #通過訪問此地址,獲取返回值,在返回值中獲取那個字段,拼接到 登錄 login.html中的 img標簽的 # src標簽里表就能在頁面中渲染出獲取的二維碼 ret=requests.get(url) ret1=ret.text #使用正則來吧數據取出來 c=re.findall('uuid = "(.*)";',ret1)[0] #當檢查是否掃碼的時候,會用到這個值,由於Flask是上下文管理的形式,能夠保存的數據只有保存在session中, session["c"]=c return render_template('login.html',c=c,) else: pass
login.html初級代碼
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1"> <title>Title</title> <style> </style> </head> <body> <div style="width: 200px;margin: 0 auto"> <h3 style="text-align: center">微信登錄</h3> <!--通過看微信網頁版登錄頁面可以得知,二維碼這個圖片的src是這個值,只是最后的數據發生了變化--> <img id="img" style="width: 200px;height: 200px" src="https://login.weixin.qq.com/qrcode/{{c}}" alt=""> </div> </body> </html>
繼續:
功能一:我這個也頁面需要實時的檢測用戶是否掃碼
功能實現
參照微信掃碼前后數據變化可以得知
點進去看一下 :這里用到了一個長輪詢的機制:優點:輪詢和長輪詢
當用戶掃碼之但是沒有在手機上確認登錄的時候
掃碼之后,但是沒有確認登錄頁面上顯示的內容如下,將用戶的頭像替代了二維碼
檢查用戶是否掃碼,是否登錄的代碼
#encoding=utf-8 from flask import Flask,request,render_template,request,session,jsonify import time import re import requests app=Flask(__name__) app.debug=True app.secret_key='aa' @app.route('/login',methods=["GET","POST"]) def login(): if request.method=="GET": ctime= time.time() a=str(int(ctime*1000)) #微信登錄頁面是 #獲取url 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&_={0}".format(a) # print('*'*50,url) #通過訪問此地址,獲取返回值,在返回值中獲取那個字段,拼接到 登錄 login.html中的 img標簽的 # src標簽里表就能在頁面中渲染出獲取的二維碼 ret=requests.get(url) ret1=ret.text #使用正則來吧數據取出來 c=re.findall('uuid = "(.*)";',ret1)[0] #當檢查是否掃碼的時候,會用到這個值,由於Flask是上下文管理的形式,能夠保存的數據只有保存在session中, session["c"]=c return render_template('login.html',c=c,) else: pass @app.route('/checklogin',methods=["GET","POST"]) def checklogin(): #這邊創建一個字典:並將code的值默認設置為408,表示用戶未掃碼, response={"code":408} import time time.sleep(2) ctime = time.time() a = str(int(ctime * 1000)) #獲取當前的時間戳的1000倍的值,這里注意要將內容轉換成字符串類型用於拼接url c=session.get("c") # print("這是獲取的ac",a,c) # URL:https://login.wx.qq.com/cgi-bin/mmwebwx-bin/login?loginicon=true&uuid=QcksT_RRig==&tip=0&r=-1055866945&_=1525769055588" url="https://login.wx.qq.com/cgi-bin/mmwebwx-bin/login?loginicon=true&uuid={0}&tip=0&r=-1055866945&_={1}".format(c,a) #去服務器端去訪問並獲取數據: 通過微信官網觀察 # 如果沒掃描,code為408 掃描未登錄 code:201 掃描登錄 code:200 # 而且這些數據是手機掃碼手機將數據發送給服務端,然后服務端改變code ret4=requests.get(url) print("這是獲取的ret",ret4.text) if "code=201" in ret4.text: print('用戶已掃碼') #從發送會的數據中獲取用戶頭像的url src=re.findall("userAvatar = '(.*)';",ret4.text) response["code"]=201 #將url保存在response中返回給客戶端 response["src"]=src # return response elif "code=200" in ret4.text: response["code"] = 200 print(ret4.text) #當用戶確定登錄后,訪問的地址 currenturl=re.findall('redirect_uri="(.*)";',ret4.text)[0]+"&fun=new&version=v2" ret5=requests.get(currenturl) print('這是ret5的內容',ret5) from bs4 import BeautifulSoup soup=BeautifulSoup(ret5.text,'html.parser') # soup.find(ret5.text,recursive) #從返回值中獲取內容,將內容拼接到字典中去,用於渲染出用戶登錄后的初始數據 error=soup.find(name='error') print(error) items=error.find_all(recursive=False) print(items) dict1={} for item in items: dict1[item.name]=item.text print(dict1) #將字典內容寫到session中去,在顯示頁面中顯示用戶的初始信息 session["dict1"]=dict1 print('登陸成功') else: print('用戶為掃碼') #返回一個json類型的數據(字典類型的) return jsonify(response) @app.route('/index',methods=["GET","POST"]) #渲染用戶信息頁面 def index(): d1=session.get('dict1') #通過視頻可以知道渲染出數據需要將資格字典穿過去, dict = {"BaseRequest":{"Uin":d1.get("wxuin"), "Sid": d1.get("wxsid"), "Skey": d1.get("skey"), "DeviceID":"e108769709273733", }} #發送一個post請求,把內容傳過去。 ret6=requests.post("https://wx.qq.com/cgi-bin/mmwebwx-bin/webwxinit?r=-1067834323&pass_ticket={0}".format(d1.get("pass_ticket")), json=dict, ) ret6.encoding=ret6.apparent_encoding import json dd=json.loads(ret6.text) print(dd) return render_template('index.html',data=dd.get('User')) if __name__ == '__main__': app.run()
lgoin html頁面
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1"> <title>Title</title> <style> </style> </head> <body> <div style="width: 200px;margin: 0 auto"> <h3 style="text-align: center">微信登錄</h3> <!--通過看微信網頁版登錄頁面可以得知,二維碼這個圖片的src是這個值,只是最后的數據發生了變化--> <img id="img" style="width: 200px;height: 200px" src="https://login.weixin.qq.com/qrcode/{{c}}" alt=""> </div> <script src="../static/jquery-1.12.4.js"></script> <script> $(function () { checklogin(); }); function checklogin() { $.ajax({ url:'/checklogin', type:'GET', // 獲取的數據類型為json格式,接受后不用再解碼 dataType:'JSON', success:function (arg) { if(arg.code===201){ console.log(arg.src); $("#img").attr("src",arg.src); checklogin() }else if(arg.code===200){ location.href='/index' }else{checklogin()} } }) } </script> <!--https://login.wx.qq.com/cgi-bin/mmwebwx-bin/login?loginicon=true&uuid=wf4SqxD0tA==&tip=0&r=-1051590008&_=1525764953929--> </body> </html>
index頁面
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1"> <title>Title</title> </head> <body> {{data.NickName}} </body> </html>