wxpy实现微信机器人

因为之前想过 如果每天早上微信能够发送天气预报给我,给我老婆多好,然后就动手看网上的教程做了一个可以定时发送天气预报的程序,

最近又想到折腾,做了一个更加详细的版本。但是需要主动操作

具体操作看图。
4bf724fa5a27670491cee4fd3b7977c.png
d3d3306aa96844c06a4dd991a398e2b.png
b0d0d26535ef1647a74fb0ec5853b5b.png

部分代码:

#coding=utf8
import requests
from requests import exceptions
from urllib.request import urlopen
from bs4 import BeautifulSoup
from urllib.parse import urlencode
from threading import Timer
import re
from wxpy import *
import  schedule
import  time
import http
import  json 
import datetime
import random

bot = Bot(cache_path=True,console_qr = 1)
myself = bot.self
bot.enable_puid('wxpy_puid.pkl')
tuling = Tuling(api_key='换成自己的图片key')
group = bot.groups().search(u'Test')
shgroup = bot.groups().search('伐木累?')
friends = bot.friends().search(u'Lie')
msgText = "Helo!  回复'功能'获取对应功能\n1.天气(例:苏州天气)\n2.今日nba(注:今日所有比赛结果)\n3.今日黄历\n4.每日一句\n5.开启机器人(关闭机器人)\n6.今日古诗词\n7.每日阅读\n8.历史上的今天\n9.nba排名(注:当日东西部排名)\n10.新闻\n          1.头条新闻\n          2.社会新闻\n          3.娱乐新闻\n          4.体育新闻\n11.星座运势(例如:天秤座)"  #任意回复获取的菜单
newText = "你可以这样回复: \n1.头条新闻\n2.社会新闻\n3.娱乐新闻\n4.体育新闻"

def get_now_weather(city):
    header = {
        'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8',
        'Accept-Encoding': 'gzip, deflate',
        'Accept-Language': 'zh-CN,zh;q=0.8',
        'Connection': 'keep-alive',
        'User-Agent': 'Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/43.0.235'
    }
    url = 'https://free-api.heweather.com/s6/weather/now?location='+city+'&key=换成自己的聚合数据key'
    PMurl = 'https://free-api.heweather.com/s6/air/now?parameters&location='+city+'&key=换成自己的聚合数据key'
    # 设定超时时间,防止被网站认为是爬虫
    timeout = random.choice(range(80, 180))
    rep = requests.get(url, headers=header, timeout=timeout)
    pm = requests.get(PMurl, headers=header, timeout=timeout)
    result = ''
    temp = rep.json()
    temp = temp['HeWeather6'][0]
    update = temp['update']
    now = temp['now']
    nowTime = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')
    pm = pm.json()
    pm = pm['HeWeather6'][0]
    print(now)
    airnow = pm['air_now_city']
    result = city +  '实时天气预报-' + '\n'\
    + '更新时间:'+ update['loc'] + '\n'\
    + '          当前天气:'+ now['cond_txt'] + '\n'\
    + '          当前温度:'+ now['tmp'] + '°C' + '\n'\
    + '          体感温度:'+ now['fl'] + '°C' + '\n'\
    + '          风向:'+ now['wind_dir'] + ' ' + now['wind_sc'] + '级 '+ now['wind_spd'] + '公里/小时'+ '\n'\
    + '          相对湿度:'+ now['hum'] + '%' + '\n'\
    + '          降水量:'+ now['pcpn'] + 'ml' + '\n'\
    + '          能见度:'+ now['vis'] + '公里' + '\n'\
    + '          云量:'+ now['cloud']  + '\n'\
    + '-----------------------------------' + '\n'\
    + '当前空气质量:'+'\n'\
    + '          空气质量指数:'+ airnow['aqi']+'\n'\
    + '          主要污染物:'+ airnow['main']+'\n'\
    + '          空气质量:'+ airnow['qlty']+'\n'\
    + '          二氧化氮指数:'+ airnow['no2']+'\n'\
    + '          二氧化硫指数:'+ airnow['so2']+'\n'\
    + '          一氧化碳指数:'+ airnow['co']+'\n'\
    + '          pm10指数:'+ airnow['pm10']+'\n'\
    + '          pm25指数:'+ airnow['pm25']+'\n'\
    + '          臭氧指数:'+ airnow['o3']+'\n'

    result =  result + '发送时间:' +  nowTime + '\n'

    return result

def get_news(type):
    header = {
        'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8',
        'Accept-Encoding': 'gzip, deflate',
        'Accept-Language': 'zh-CN,zh;q=0.8',
        'Connection': 'keep-alive',
        'User-Agent': 'Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/43.0.235'
    }
    url = 'http://v.juhe.cn/toutiao/index?type='+str(type)+'&key=换成自己的聚合数据key'
    timeout = random.choice(range(80, 180))
    rep = requests.get(url, headers=header, timeout=timeout)
    data = json.loads(rep.text)
    data = data['result']
    data = data['data']
    item = []
    obj = {}
    html = '今日'+str(type)+'新闻:'+ '\n'
    for i in data:
        html = html + '标题:' + i['title'] + '\n'\
                + '链接:' + i['url'] + '\n'\
                + '分类:' + i['category'] + '\n'\
                + '来自:' + i['author_name'] + '\n'\
                + '时间:' + i['date'] + '\n'\
                + '-----------------------------------------------' + '\n' +'\n' \

    return html

def get_star(name):
    header = {
        'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8',
        'Accept-Encoding': 'gzip, deflate',
        'Accept-Language': 'zh-CN,zh;q=0.8',
        'Connection': 'keep-alive',
        'User-Agent': 'Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/43.0.235'
    }
    url = 'http://web.juhe.cn:8080/constellation/getAll?consName='+str(name)+'&type=today&key=换成自己的聚合数据key'
    timeout = random.choice(range(80, 180))
    rep = requests.get(url, headers=header, timeout=timeout)
    data = json.loads(rep.text)
    starhtml = '今日'+str(name)+'运势:'+ '\n'\
        + '          综合指数:' + data['all'] + '\n'\
        + '          幸运色:' + data['color'] + '\n'\
        + '          健康指数:' + data['health'] + '\n'\
        + '          爱情指数:' + data['love'] + '\n'\
        + '          财运指数:' + data['money'] + '\n'\
        + '          速配星座:' + data['QFriend'] + '\n'\
        + '          工作指数:' + data['work'] + '\n'\
        + '          今日概述:' + data['summary'] + '\n'\

    return starhtml

def get_nba():
    resp = urlopen('https://m.hupu.com/nba/game')
    soup = BeautifulSoup(resp,'html.parser')
    tagToday = soup.find('section',class_="match-today")
    nbaHtml = '今日NBA比赛结果:' + '\n' + '\n'
    for tag in tagToday.find_all('a', class_='match-wrap'): 
        nbaHtml = nbaHtml + tag.find('div', class_='away-team').span.get_text() + '    ' + tag.find('strong', class_='').span.get_text() + '    ' + tag.find('div', class_='home-team').span.get_text() + '  (' + tag.find('div', class_='match-status-txt').get_text() +')' + '\n'

    return nbaHtml  

def get_rank():
    resp = urlopen('https://m.hupu.com/nba/stats')
    soup = BeautifulSoup(resp,'html.parser')
    east = soup.find_all('li',class_= "weast")[0]
    west = soup.find_all('li',class_= "weast")[1]
    rankHtml = '今日NBA东部排名:(1.排名  2.球队  3.胜负  4.胜负差  5.最近情况)' + '\n' + '\n'
    for tag in east.find_all('li', class_=''): 
        list = tag.find('p', class_='right-data')
        rankHtml = rankHtml + tag.find('span', class_='rank').get_text() + '. ' + tag.find('div', class_='').h1.get_text() + '    ' + list.find_all('span')[0].get_text() + '    ' + list.find_all('span')[1].get_text() +'    '+ list.find_all('span')[2].get_text() +'\n'

    rankHtml = rankHtml + '\n' + '\n' + '---------------------------------------------' + '\n' + '\n'
    rankHtml = rankHtml + '今日NBA西部排名:(1.排名  2.球队  3.胜负  4.胜负差  5.最近情况)' + '\n' + '\n'        
    for tag in west.find_all('li', class_=''): 
        list = tag.find('p', class_='right-data')
        rankHtml = rankHtml + tag.find('span', class_='rank').get_text() + '. ' + tag.find('div', class_='').h1.get_text() + '    ' + list.find_all('span')[0].get_text() + '    ' + list.find_all('span')[1].get_text() +'    '+ list.find_all('span')[2].get_text() +'\n'
    
    return rankHtml   

def invite(user):
    print('4')
    group =  bot.groups().search('cc')
    group[0].add_members(user, use_invitation=True)


@bot.register(msg_types=FRIENDS)
@bot.register(group)
@bot.register(shgroup,TEXT)
@bot.register(friends)

喜欢的可以 关注我的公众号 【 执行上下文】 发送 wxpy 获取源码,或者加微信 jiaopianbot 验证信息【 py】 拉你进群体验

或者直接扫描二维码

WeChat Image_20181221112044.jpg

关注我的公众号 【 执行上下文】 发送 wxpy 获取源码在这里插入图片描述

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值