python基于新浪sae開(kāi)發(fā)的微信公眾平臺(tái),實(shí)現(xiàn)功能:
輸入段子---回復(fù)笑話(huà)
輸入開(kāi)源+文章---發(fā)送消息到開(kāi)源中國(guó)
輸入快遞+訂單號(hào)---查詢(xún)快遞信息
輸入天氣---查詢(xún)南京最近五天天氣狀況
輸入微博熱點(diǎn)---回復(fù)微博當(dāng)前熱門(mén)話(huà)題
輸入電影+名稱(chēng)---回復(fù)百度云盤(pán)中搜索的鏈接
具體實(shí)現(xiàn)代碼:
# -*- 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): #獲取輸入?yún)?shù) data = web.input() signature=data.signature timestamp=data.timestamp nonce=data.nonce echostr=data.echostr #自己的token token="weixin9047" #這里改寫(xiě)你在微信公眾平臺(tái)里輸入的token #字典序排序 list=[token,timestamp,nonce] list.sort() sha1=hashlib.sha1() map(sha1.update,list) hashcode=sha1.hexdigest() #sha1加密算法 #如果是來(lái)自微信的請(qǐng)求,則回復(fù)echostr if hashcode == signature: return echostr def POST(self): str_xml = web.data() #獲得post來(lái)的數(shù)據(jù) xml = etree.fromstring(str_xml)#進(jìn)行XML解析 content=xml.find("Content").text#獲得用戶(hù)所輸入的內(nèi)容 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}(?=
更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主
微信掃碼或搜索:z360901061

微信掃一掃加我為好友
QQ號(hào)聯(lián)系: 360901061
您的支持是博主寫(xiě)作最大的動(dòng)力,如果您喜歡我的文章,感覺(jué)我的文章對(duì)您有幫助,請(qǐng)用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點(diǎn)擊下面給點(diǎn)支持吧,站長(zhǎng)非常感激您!手機(jī)微信長(zhǎng)按不能支付解決辦法:請(qǐng)將微信支付二維碼保存到相冊(cè),切換到微信,然后點(diǎn)擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對(duì)您有幫助就好】元
