37创客科创中心

 找回密码
 立即注册
查看: 1950|回复: 2

微信

[复制链接]

194

主题

324

帖子

2401

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
2401
发表于 2024-1-27 11:07:24 | 显示全部楼层 |阅读模式






分享名称:wx
分享链接:http://p.37ck.cn/#s/-Exof2xw
访问密码:wxgzh



分享名称:微信公众号webpy
分享链接:http://p.37ck.cn/#s/-F1-0O4Q
访问密码:20200808

handle.py
  1. # encoding: UTF-8
  2. import hashlib
  3. import web
  4. import receive
  5. import reply
  6. class Handle:
  7.     def GET(self):
  8.         '''
  9.         验证是否为微信服务器发送过来的消息
  10.         :return:echostr
  11.         '''
  12.         web.header('Content-Type', 'text/html;charset=UTF-8')#转码为中文显示
  13.         try:
  14.             data = web.input()
  15.             if len(data) == 0:
  16.                 return "37ck 20240201创客中心"

  17.             signature = data.signature
  18.             timpestamp = data.timestamp
  19.             noce = data.once
  20.             echostr = data.echostr
  21.             token = "37ck20200808"  # 请按照公众平台官网\基本配置中信息填写

  22.             list = [token, timpestamp, noce]
  23.             list.sort()
  24.             sha1 = hashlib.sha1()
  25.             map(sha1.update, list)
  26.             hashcode = sha1.hexdigest()
  27.             print
  28.             "handle/GET func:hashcode,signature:", hashcode, signature
  29.             if hashcode == signature:
  30.                 return echostr
  31.             else:
  32.                 return "验证签名错误"

  33.         except Exception as Argument:
  34.             return Argument

  35.     def POST(self):
  36.         web.header('Content-Type', 'text/html;charset=UTF-8') #
  37.         try:
  38.             webData = web.data()
  39.             print
  40.             "Handle Post webdata is ", webData  # 后台打印日志
  41.             recMsg = receive.parse_xml(webData)

  42.             if isinstance(recMsg, receive.Msg):
  43.                 # 实现粉丝号发送文本消息来,公众号回复test文本给粉丝号
  44.                 toUser = recMsg.FromUserName
  45.                 fromUser = recMsg.ToUserName

  46.                 if recMsg.MsgType == "text":
  47.                     content = "37创客欢迎你"
  48.                     replyMsg = reply.TextMsg(toUser, fromUser, content)
  49.                     return replyMsg.send()
  50.                 elif recMsg.MsgType == "image":
  51.                     mediaId = recMsg.MediaId
  52.                     replyMsg = reply.ImageMsg(toUser, fromUser, mediaId)
  53.                     return replyMsg.send()
  54.                 else:
  55.                     return reply.Msg().send()
  56.             else:
  57.                 print
  58.                 "暂且不处理"
  59.                 return reply.Msg().send()

  60.         except Exception as Argment:
  61.             return Argment
复制代码













本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

x
回复

使用道具 举报

194

主题

324

帖子

2401

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
2401
 楼主| 发表于 2024-2-2 10:56:42 | 显示全部楼层
import requests
from# lxml import etree
Idef get_weather(keyword): 1个用法
url =https://www.tiangi.com/tiangi/search?keyword=' + keyword
headers ={
.'User-Agent': 'Mozilla/5.0 (windows NT 10.0, Win64, x64) ApplewebKit/537.36 (KHTML,like Gecko) chrome/68.0.3440.106 Safari/537.36'
response= requests.get(url,headers=headers)
tree = etree.HTML(response.text)
#⑧擊钼贰锾测城市天气是否存在
try:
city_name = tree.xpath('//dd[@class="name"]/h2/text()')[0]
except:
content="没有该城市天气信息,请确认查询格式!
return content
week = tree.xpath('//dd[@class="week"]/text()')[0]
now = tree.xpath('//p[@class="now"]')[0].xpath('string(.)')
temp = tree.xpath('//dd[@class="weather"]/span')[0].xpath('string(.)')
shidu =tree.xpath("//dd[@class="shidu"]/b/text()')
konggi = tree.xpath('//dd[@class="kongqi"]/h5/text()')[0]
pm=tree.xpath('//dd[@class="kongqi"]/hó/text()')[0]
content ="【f0}】{1}天气(n兰前温度:2}n今日天气: 3hn4}ntshnt6}".fomat(city mname, week.split("u300')[0],now,temp,'in'.join(shidu),kongqi,pm)
return content
Jif name   == "__main  ":
keyword ='北京'
content = get_weather(keyword)
print(content)
回复

使用道具 举报

194

主题

324

帖子

2401

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
2401
 楼主| 发表于 2024-2-4 09:56:15 | 显示全部楼层
  1. # -*- coding: utf-8 -*-
  2. #app.py
  3. from flask import Flask, request
  4. from flask import make_response
  5. import hashlib
  6. from wechatpy import parse_message, create_reply

  7. # 实例化Flask
  8. app = Flask(__name__)
  9. global reply


  10. @app.route('/wx', methods=['GET', 'POST'])  # 路由
  11. def wx0204():
  12.     if request.method == 'GET':
  13.         token = '37ck20200808'
  14.         data = request.args
  15.         signature = data.get('signature', '')
  16.         timestamp = data.get('timestamp', '')
  17.         nonce = data.get('nonce', '')
  18.         echostr = data.get('echostr', '')
  19.         s = sorted([timestamp, nonce, token])
  20.         # 字典排序
  21.         s = ''.join(s)
  22.         if hashlib.sha1(s.encode('utf-8')).hexdigest() == signature:
  23.             response = make_response(echostr)
  24.             return response
  25.         # return '37创客:错误签名'
  26.     else:
  27.         msg = parse_message(request.get_data())
  28.         # msg = parse_message(request.args)
  29.         # msg = request.args
  30.         if msg.type == 'text':
  31.             reply = create_reply('你发送了条文字信息:' + msg.content, message=msg)
  32.         if msg.type == 'image':
  33.             reply = create_reply('你发送了条图片信息', msg)
  34.         if msg.content == '37ck':
  35.             reply = create_reply('你发送了条文字信息:', '37创客')
  36.         return reply.render()


  37. # 主程序
  38. if __name__ == '__main__':
  39.     app.run(host='0.0.0.0', port=84, debug=True)
复制代码
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|手机版|小黑屋|37创客科创中心

GMT+8, 2025-12-11 17:19 , Processed in 0.196100 second(s), 19 queries .

Powered by Discuz! X3.4

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表