日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪(fǎng)問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程语言 > python >内容正文

python

python 发送微信公众号消息_Python 微信公众号发送消息

發(fā)布時(shí)間:2023/12/20 python 37 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python 发送微信公众号消息_Python 微信公众号发送消息 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

#_*_coding:utf-8_*_

import requests

import json

import MySQLdb

def get_access_token():

"""

獲取微信全局接口的憑證(默認(rèn)有效期倆個(gè)小時(shí))

如果不每天請(qǐng)求次數(shù)過(guò)多, 通過(guò)設(shè)置緩存即可

"""

result = requests.get(

url="https://api.weixin.qq.com/cgi-bin/token",

params={

"grant_type": "client_credential",

"appid": "xxxxxxxxx", # 微信公眾號(hào)appid

"secret": "xxxxxxxx", # 微信公眾號(hào)secret

}

).json()

print(result)

if result.get("access_token"):

access_token = result.get("access_token")

else:

access_token = None

return access_token

def sendmsg(openid,msg):

access_token = get_access_token()

body = {

"touser": openid,

"msgtype": "text",

"text": {

"content": msg

}

}

response = requests.post(

url="https://api.weixin.qq.com/cgi-bin/message/custom/send",

params={

"access_token": access_token

},

data=bytes(json.dumps(body, ensure_ascii=False), encoding="utf-8")

)

# 這里可根據(jù)回執(zhí)code進(jìn)行判定是否發(fā)送成功(也可以根據(jù)code根據(jù)錯(cuò)誤信息)

result = response.json()

print(result)

path = r"C:\\Users\\Administrator\\Desktop\\weixingong\\user\\%s.txt" % (openid)

print(result["errcode"])

try:

db = MySQLdb.connect(host="192.168.1.4", user="root", db="public_opinion", port=3306, passwd="123456",

charset="utf8")

except Exception as e:

print(e, "數(shù)據(jù)庫(kù)連接錯(cuò)誤")

if result["errcode"] == 0:

with open(path, "a+") as f:

f.write("1")

f.close()

cursor = db.cursor()

# 使用execute方法執(zhí)行SQL語(yǔ)句

sql = "update weixin_user set state = '%s' where userid = '%s'" % (str(result["errcode"]),openid)

print(sql)

cursor.execute(sql)

# 提交sql事五

db.commit()

# 關(guān)閉本次操作

cursor.close()

else:

cursor = db.cursor()

# 使用execute方法執(zhí)行SQL語(yǔ)句

sql = "update weixin_user set state = '%s' where userid = '%s'" % (str(result["errcode"]),openid)

print(sql)

cursor.execute(sql)

# 提交sql事五

db.commit()

# 關(guān)閉本次操作

cursor.close()

if __name__ == "__main__":

sendmsg("ofDDs0SP9rsDT_pupCsSSgagVVu8","你好?")

# 參數(shù)1,要發(fā)送的用戶(hù)tofakeid 微信公眾號(hào)點(diǎn)擊用戶(hù)頭像url連接中有

# 參數(shù)2,要發(fā)送的內(nèi)容

總結(jié)

以上是生活随笔為你收集整理的python 发送微信公众号消息_Python 微信公众号发送消息的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。