爬取一个天气预报结合微信公总号发送

最近看见我的一个朋友些的一个爬取天气预报的爬虫不错,后来发现每次执行发送的时候非常的不方便,每次都要扫描二维码,就想起了以前zabbi公总号的方法传送天气预报信息:


/test cat weather_wechat.py
#!/usr/local/bin/python
# -*- coding: utf-8 -*-
import requests
import json
import sys
reload(sys)
sys.setdefaultencoding('utf-8')
import urllib3
import time
import re
import urllib2
hearders = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36"
url = "https://tianqi.moji.com/weather/china/guangdong/guangzhou"    ##要爬去天气预报的网址
par = '(<meta name="description" content=")(.*?)(">)'    ##正则匹配,匹配出网页内要的内容
opener = urllib2.build_opener()
opener.addheaders = [hearders]
urllib2.install_opener(opener)
##获取网页
html = urllib2.urlopen(url).read().decode("utf-8")
##提取需要爬取的内容
cml = re.search(par,html).group(2)
class weChat:
    def __init__(self,Corpid,Secret):
        url = 'https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=%s&corpsecret=%s' % (Corpid,Secret)
        res = self.url_req(url)
        self.token = res["access_token"]
    def url_req(self,url):
        requests.packages.urllib3.disable_warnings()
        req = requests.get(url, verify=False)
        res = json.loads(req.text)
        return res
    def send_message(self,user,content):
        url = "https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token=%s" % self.token
        data = {
                "touser": user,
                "msgtype": "text",
                "agentid": 1000009,
                "text": {
                        "content": content
                        },
                "safe":"0"
                }
        requests.packages.urllib3.disable_warnings()
        res = requests.post(url,json=data, verify=False)
        if json.loads(res.content)['errmsg'] == 'ok':
            return "send message sucessed"
        else:
            return res
localtime = time.strftime("%Y-%m-%d %H:%M:%S")
a = "天气预报:%s" %(localtime)
if __name__ == '__main__':
    user = '@all'
    title = a
    msg = cml
    content = a + '\n' + cml
    get_token = weChat('wxasdadsasafdd7ae','A52vcpd2z4ASDSADASDSAR_vug8')
    print get_token.send_message(user, content)
 /test sudo python weather_wechat.py
send message sucessed


爬取一个天气预报结合微信公总号发送

爬取一个天气预报结合微信公总号发送

上一篇:微信小程序 自定义组件(stepper)


下一篇:微信小程序-布局