有时候回家偶尔经过彩票店,也会花个小钱买个希望,嘿嘿。不过更多的是忘记去看看有没中奖。想想还是写个爬虫,爬下500万彩票网的大乐透和双色球信息吧。通过调用企业微信来推送。。。阿门,什么时候来个大奖砸晕我吧。
参考http://www.cnblogs.com/Eric-Young/p/7474729.html Python3调用企业微信用于告警
脚本如下:
from lxml import etree import urllib.request import datetime,json
# 获取彩票网站信息 def get_xinfo(url,headers): req = urllib.request.Request(url=url,headers=headers) #伪装浏览器 res = urllib.request.urlopen(req).read().decode('gb2312') #通过 F12分析得知是 gb2312 html = etree.HTML(res) title = html.xpath('//div[@class="kjxq_box02_title_left"]/img/@alt') riqi = html.xpath('//font[@class="cfont2"]/strong/text()') num = html.xpath('//div[@class="ball_box01"]/ul/li/text()') msg = "%s\n第 %s期\n开奖号码: %s %s %s %s %s %s %s"%(title[0],riqi[0],num[0],num[1],num[2],num[3],num[4],num[5],num[6]) # 总感觉这一句写的有点low return msg
#编辑微信post数据 def messages(msg): values = { "touser": 'user1', "msgtype": 'text', "agentid": 1, "text": {'content': msg}, "safe": 0 } msges=(bytes(json.dumps(values), 'utf-8')) return msges
#制作微信url def weixin(headers,CropID,Secret,msg): GURL = "https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=%s&corpsecret=%s"%(CropID,Secret) req = urllib.request.Request(url=GURL,headers=headers) Gtoken = urllib.request.urlopen(req).read().decode('utf-8') Gtoken = json.loads(Gtoken) access_token = Gtoken['access_token'] PURL = "https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token=%s"%access_token res = urllib.request.urlopen(urllib.request.Request(url=PURL, data=messages(msg))).read() x = json.loads(res.decode())['errcode'] if x == 0: print('Successfully') else: print('Failed')
#主入口,判断是星期几 if __name__ == '__main__': date = ['0','2','4'] week = datetime.date.today().strftime('%w') headers = { 'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.113 Safari/537.36', 'Connection': 'keep-alive'} CropID = 'asdasjkldqwjiodasd1' # 瞎打的 Secret = '-asdasklaskjldaqdqnkladsdkldasdjakldqj' if week in date: url = 'http://kaijiang.500.com/ssq.shtml' # 双色球 [每周二、四、日开奖] else: url = 'http://kaijiang.500.com/dlt.shtml' # 大乐透 [每周一、三、六开奖] msg = get_xinfo(url,headers) weixin(headers,CropID,Secret,msg) |
结果截图: