Python is based on the public platform developed by Sina SAE to realize its functions:
Input Satin---Reply to jokes
Enter open source + article---Send message to open source China
Enter The Courier + Order number---Query The Courier information
Enter Weather---Query weather conditions in Nanjing last five days
Enter Weibo hotspot---Reply to the current hot topic of Weibo
Enter movie + Name---Reply to Baidu Cloud disk search link
Specific implementation code:
#-*-Coding:utf-8-*-import hashlibimport webimport lxmlimport timeimport osimport urllib2,jsonimport urllibimport reimp ORT randomimport hashlibimport cookielibfrom urllib import urlencodefrom 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 () sig Nature=data.signature timestamp=data.timestamp nonce=data.nonce echostr=data.echostr #自己的token token= "Weixi n9047 "#这里改写你在公众平台里输入的token #字典序排序 list=[token,timestamp,nonce] List.sort () SHA1=HASHLIB.SHA1 () map (sha1.up date,list) hashcode=sha1.hexdigest () #sha1加密算法 #如果是来自的请求, reply 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# gets the content entered by the user msgtype=xml.find (" Msgtype "). Text Fromuser=xml.find (" Fromusername "). Text Touser=xml.find (" Tous Ername "). text if (content = = u" Weather "): url =" http://m.ip138.com/21/nanjing/tianqi/"headers = {' Connect Ion ': ' 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} (? =) ' 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 ()), "last five days weather : \ n "+str_wether) elif (Content[0:2] = = u "movie"): keyword = urllib.quote (Content[2:].encode ("Utf-8")) URL = "http://www.wangpansou.cn/s.ph p?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 "The following is the movie link: \ n" +string) elif (U "in content): Url_8 =" Http://www.qiu shibaike.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 (Wi Ndows 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) HTM L_8 = Opener_8.read () html_24 = Opener_24.read () Rex = R ' (? <=div class= "Content" >). *? (?=