获取用户的个人信息,联系人列表,公众号
1、页面展示,templates/index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Wechat</title>
</head>
<body>
<h1>个人信息</h1>
<div>
<img src="https://wx.qq.com{{data.User.HeadImgUrl}}"/>
</div>
<div>
{{data.User.NickName}} - {{data.User.UserName}}
</div>
<h1>联系人列表</h1>
<ul>
{% for row in data.ContactList%}
<li>{{row.UserName}}-{{row.NickName}}</li>
{% endfor %}
</ul>
<h1>公众号</h1><ul>
{% for row in data.MPSubscribeMsgList%}
<div style="font-weight: bolder">{{row.NickName}}</div>
{% for i in row.MPArticleList %}
<div>
<div><a href="{{i.Url}}">{{i.Title}}</a></div>
<div style="color: #dddddd">{{i.Digest}}</div>
</div>
{% endfor %}
{% endfor %}
{{data}}
</body>
</html>
2、后台返回的用户初始信息
def index(request):
"""微信登陆的页面初始化,获取用户的基本信息"""
#user_init_url = "https://wx2.qq.com/cgi-bin/mmwebwx-bin/webwxinit?pass_ticket=%s&r=%s" %(TICKET_DATA_DICT["pass_ticket"],int(time.time()))
user_init_url = "https://wx2.qq.com/cgi-bin/mmwebwx-bin/webwxinit?r=-631178899"
payload_data = {
"BaseRequest":{
"DeviceID":"e379444626462097",
"Sid":TICKET_DATA_DICT["wxsid"],
"Skey":TICKET_DATA_DICT["skey"],
"Uin":TICKET_DATA_DICT["wxuin"]}
}
cookie_all = {}
#因为不知道用哪个cookie所以上面两个都给加上了
cookie_all.update(LOGIN_COOKIE_DICT)
cookie_all.update(TICKET_COOKIE_DICT)
#返回的内容是用户的信息
reponse_init=requests.post(url=user_init_url,json=payload_data,cookies=cookie_all)
reponse_init.encoding="utf-8"
#用户信息转成dict
reponse_init_data = json.loads(reponse_init.text)
#print("reponse_init",reponse_init.text)
# print("------------------------------------------")
# for k,v in reponse_init_data.items():
# print(k,v)
#把数据都保留在这个全局变量中
USER_INIT_DATA.update(reponse_init_data)
return render(request,"index.html",{"data":reponse_init_data,})