python搭建微信公众平台
作者:lijiao 发布时间:2022-05-24 00:14:09
标签:python,微信
python基于新浪sae开发的微信公众平台,实现功能:
输入段子---回复笑话
输入开源+文章---发送消息到开源中国
输入快递+订单号---查询快递信息
输入天气---查询南京最近五天天气状况
输入微博热点---回复微博当前热门话题
输入电影+名称---回复百度云盘中搜索的链接
具体实现代码:
# -*- coding: utf-8 -*-
import hashlib
import web
import lxml
import time
import os
import urllib2,json
import urllib
import re
import random
import hashlib
import cookielib
from urllib import urlencode
from lxml import etree
class WeixinInterface:
def __init__(self):
self.app_root = os.path.dirname(__file__)
self.templates_root = os.path.join(self.app_root, 'templates')
self.render = web.template.render(self.templates_root)
def GET(self):
#获取输入参数
data = web.input()
signature=data.signature
timestamp=data.timestamp
nonce=data.nonce
echostr=data.echostr
#自己的token
token="weixin9047" #这里改写你在微信公众平台里输入的token
#字典序排序
list=[token,timestamp,nonce]
list.sort()
sha1=hashlib.sha1()
map(sha1.update,list)
hashcode=sha1.hexdigest()
#sha1加密算法
#如果是来自微信的请求,则回复echostr
if hashcode == signature:
return echostr
def POST(self):
str_xml = web.data() #获得post来的数据
xml = etree.fromstring(str_xml)#进行XML解析
content=xml.find("Content").text#获得用户所输入的内容
msgType=xml.find("MsgType").text
fromUser=xml.find("FromUserName").text
toUser=xml.find("ToUserName").text
if(content == u"天气"):
url = "http://m.ip138.com/21/nanjing/tianqi/"
headers = {
'Connection': 'Keep-Alive',
'Accept': 'text/html, application/xhtml+xml, */*',
'Accept-Language': 'en-US,en;q=0.8,zh-Hans-CN;q=0.5,zh-Hans;q=0.3',
'User-Agent': 'Mozilla/5.0 (Windows NT 6.3; WOW64; Trident/7.0; rv:11.0) like Gecko'}
req = urllib2.Request(url, headers = headers)
opener = urllib2.urlopen(req)
html = opener.read()
rex = r'(?<=img src="/image/s[0-9].gif" alt=").{1,6}(?=" />)'
rexx = r'(?<=div class="temperature">).{5,15}(?=</div>)'
n = re.findall(rex,html)
m = re.findall(rexx,html)
str_wether = ""
for (i,j) in zip(m,n):
str_wether = str_wether + j + " " +i + "\n"
return self.render.reply_text(fromUser,toUser,int(time.time()),"最近五天天气:\n"+str_wether)
elif(content[0:2] == u"电影"):
keyword = urllib.quote(content[2:].encode("utf-8"))
url = "http://www.wangpansou.cn/s.php?q="+keyword
headers = {
'Connection': 'Keep-Alive',
'Accept': 'text/html, application/xhtml+xml, */*',
'Accept-Language': 'en-US,en;q=0.8,zh-Hans-CN;q=0.5,zh-Hans;q=0.3',
'User-Agent': 'Mozilla/5.0 (Windows NT 6.3; WOW64; Trident/7.0; rv:11.0) like Gecko'}
req = urllib2.Request(url, headers = headers)
opener = urllib2.urlopen(req)
html = opener.read()
rex = r'https?://pan.baidu.com.*\?uk=[0-9]{10}.*[\d+?]"'
m = re.findall(rex,html)
string = u""
for i in m:
string = string + i + "\n"
return self.render.reply_text(fromUser,toUser,int(time.time()),u"以下是电影链接:\n"+string)
elif(u"段子" in content):
url_8 = "http://www.qiushibaike.com/"
url_24 = "http://www.qiushibaike.com/hot/"
headers = {
'Connection': 'Keep-Alive',
'Accept': 'text/html, application/xhtml+xml, */*',
'Accept-Language': 'en-US,en;q=0.8,zh-Hans-CN;q=0.5,zh-Hans;q=0.3',
'User-Agent': 'Mozilla/5.0 (Windows NT 6.3; WOW64; Trident/7.0; rv:11.0) like Gecko'}
req_8 = urllib2.Request(url_8, headers = headers)
req_24 = urllib2.Request(url_24,headers = headers)
opener_8 = urllib2.urlopen(req_8)
opener_24 = urllib2.urlopen(req_24)
html_8 = opener_8.read()
html_24 = opener_24.read()
rex = r'(?<=div class="content">).*?(?=<!--)'
m_8 = re.findall(rex,html_8,re.S)
m_24 = re.findall(rex, html_24, re.S)
m_8.extend(m_24)
random.shuffle(m_8)
return self.render.reply_text(fromUser,toUser,int(time.time()),m_8[0].replace('<br/>',''))
elif(content[0:2] == u"开源"):
url = "https://www.oschina.net/action/user/hash_login"
urll = "http://www.oschina.net/action/tweet/pub"
username = "904727147@qq.com"
passw = ""
password = hashlib.sha1(passw).hexdigest()
cj = cookielib.CookieJar()
opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj))
opener.addheaders = [('User-agent','Mozilla/5.0 (X11; Linux x86_64; rv:38.0) Gecko/20100101 Firefox/38.0 Iceweasel/38.3.0')]
urllib2.install_opener(opener)
data = {'email':username,'pwd':password}
data_post = urllib.urlencode(data)
opener.open(url, data_post)
user = "2391943"
msg = content[2:].encode("utf-8")
user_code = "lPFz26r3ZIa1e3KyIWlzPNpJlaEmZqyh6dAWAotd"
post = {'user_code':user_code,'user':user,'msg':msg}
msg_post = urllib.urlencode(post)
html = urllib2.urlopen(urll,msg_post).read()
return self.render.reply_text(fromUser,toUser,int(time.time()),u"发送到开源中国动弹成功!")
elif(content[0:2] == u"快递"):
keyword = content[2:]
url = "http://www.kuaidi100.com/autonumber/autoComNum?text="+keyword
cj = cookielib.CookieJar()
opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj))
opener.addheaders = [('User-agent','Mozilla/5.0 (X11; Linux x86_64; rv:38.0) Gecko/20100101 Firefox/38.0 Iceweasel/38.3.0')]
urllib2.install_opener(opener)
html = urllib2.urlopen(url).read()
jo = json.loads(html)
typ = jo["auto"][0]['comCode']
if(typ is None):
return self.render.reply_text(fromUser,toUser,int(time.time()),u"请检查你的定单号!")
urll = "http://www.kuaidi100.com/query?type="+typ+"&postid="+keyword
html_end = urllib2.urlopen(urll).read()
jo_end = json.loads(html_end)
if(jo_end["status"] == "201"):
return self.render.reply_text(fromUser,toUser,int(time.time()),u"订单号输入有误,请重新输入!")
text = jo_end["data"]
string = u""
for i in text:
string = string + i["time"] + i["context"] + "\n"
return self.render.reply_text(fromUser,toUser,int(time.time()),string)
elif(content == u"微博热点"):
url = "http://weibo.cn/pub/?tf=5_005"
headers = {
'Connection': 'Keep-Alive',
'Accept': 'text/html, application/xhtml+xml, */*',
'Accept-Language': 'en-US,en;q=0.8,zh-Hans-CN;q=0.5,zh-Hans;q=0.3',
'User-Agent': 'Mozilla/5.0 (Windows NT 6.3; WOW64; Trident/7.0; rv:11.0) like Gecko'}
req = urllib2.Request(url, headers = headers)
opener = urllib2.urlopen(req)
html = opener.read().decode("utf-8")
rex = r'(?<=div class="c"><a href=").{60,79}(?=</a>)'
ss = re.findall(rex,html)
string = u""
for i in ss:
string = string + i.replace('>','\n')+"\n"
return self.render.reply_text(fromUser,toUser,int(time.time()),string.replace('"',''))
elif(content == u"知乎信息"):
username = '18362983803'
password = ''
_xsrf='558c1b60725377c5810ae2484b26781e'
url = r'https://www.zhihu.com/login/phone_num'
cj = cookielib.CookieJar()
opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj))
opener.addheaders = [('User-agent','Mozilla/5.0 (X11; Linux x86_64; rv:38.0) Gecko/20100101 Firefox/38.0 Iceweasel/38.3.0')]
data = urllib.urlencode({"phone_num":username,"password":password,'_xsrf':_xsrf})
opener.open(url,data)
html = opener.open('https://www.zhihu.com/noti7/new?r=1454793308655').read()
jo = json.loads(html)
data = jo[1]
string = "增长了:"+str(data[0])+"个评论"+str(data[1])+"个粉丝"+str(data[2])+"个赞同"
return self.render.reply_text(fromUser,toUser,int(time.time()),string)
elif(u"钟志远" in content):
return self.render.reply_text(fromUser,toUser,int(time.time()),u"你想找全世界最帅的人干嘛?如果你是妹子,请加微信18362983803!汉子绕道!")
elif(u"使用" in content):
return self.render.reply_text(fromUser,toUser,int(time.time()),u"搜电影:电影+电影名,最近天气:天气,微博热门:微博热点,快递查询:快递+单号,看笑话:段子,发送动弹到开源中国:开源+内容")
else:
url = r'http://www.xiaohuangji.com/ajax.php'
cj = cookielib.CookieJar()
opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj))
opener.addheaders = [('User-agent','Mozilla/5.0 (X11; Linux x86_64; rv:38.0) Gecko/20100101 Firefox/38.0 Iceweasel/38.3.0')]
string = urllib.quote(content.encode("utf-8"))
try:
data = urllib.urlencode({"para":string})
html = opener.open(url,data).read()
string = html+"\n----[回复[使用]]"
return self.render.reply_text(fromUser,toUser,int(time.time()),string)
except Exception,ex:
return self.render.reply_text(fromUser,toUser,int(time.time()),u"我不想理你了~")


猜你喜欢
- 0、问题在用Tkinter进行编程时,需要在一个Frame下显示多个图片,但是不管怎么设置都是只显示最后一张,就像这样:代码for i in
- 程序设计是困难的,其核心是管理的复杂性。计算机程序是人类做出的最复杂的东西。质量是不可靠的且隐蔽的。好的体系架构是必需给程序足够的结构使其健
- 基数排序法又称桶子法(bucket sort)或bin sort,顾名思义,它是透过键值的部份资讯,将要排序的元素分配至某些"桶&
- 这篇文章主要介绍了python装饰器使用实例详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以
- 1.数据的增删改查----------增加数据在视图函数中导入User模型类,然后使用下面的方法添加数据:from django.http
- 1.创建tfrecordtfrecord支持写入三种格式的数据:string,int64,float32,以列表的形式分别通过tf.trai
- 代码中用到了nextpow2,其中n = nextpow2(x) 表示最接近x的2的n次幂。#!/usr/bin/env pythonimp
- 前言体能状态先于精神状态,习惯先于决心,聚焦先于喜好。SHA算法简介1.1 概述SHA (Secure Hash Algorithm,译作安
- 今天的文章来自 盏茶作酒 同学。他在老电脑中发现了一个加密的 zip 文件,于是用 Python 破解了文件密码。在破解的过程中出现了内存爆
- 前言注释可以起到一个备注的作用,团队合作的时候,个人编写的代码经常会被多人调用,为了让别人能更容易理解代码的通途,使用注释是非常有效的。Py
- 笔者在网上找了很多关于VSCODE配置Go语言的教程,但是由于版本等种种问题,最终都已失败告终。无奈只能在官方文档上寻求帮助,现在终于可以了
- 建立网站时,有很多理由让你遵循Web标准。企业、开发人员和用户都会从兼容标准的做法中获益。这里列举了一些最令人信服的理由,为什么所有网站都应
- 一、背景本文章主要是分享如何使用Python从MySQL数据库中面抽取试题,生成的试卷每一份都不一样。二、准备工作1.安装Python3下载
- 以下是Python基础学习内容的学习笔记的全部内容,非常的详细,如果你对Python语言感兴趣,并且针对性的系统学习一下基础语言知识,下面的
- 一、地理编码与逆编码地理编码与逆编码表示的是地名地址与地理坐标(经纬度)互相转换的过程。其中,将地址信息映射为地理坐标的过程称之为地理编码;
- 数据库优化有很多可以讲,按照支撑的数据量来分可以分为两个阶段:单机数据库和分库分表,前者一般可以支撑500W或者10G以内的数据,超过这个值
- 本文实例讲述了python实现根据ip地址反向查找主机名称的方法。分享给大家供大家参考。具体如下:import sys, sockettry
- sql server 锁定模式有三种:共享( S锁),更新(U锁),排他(X锁);S锁是共享锁,如果事务T对数据A加上共享锁后,则其他事务只
- 一、互联网人的焦虑互联网人是最焦虑的那批人,也是最爱学习的那批人。没办法,互联网行业的节奏实在太快了,每天都生活在信息 * 的环境里,“风口”
- 在Python中,最基本的数据结构为序列。Python中包含6种内建序列:字符串、列表、元组、Unicode字符串、buffer对象、xra