Python IDchat 微信接口发消息1

Python IDchat 微信接口发消息

1号程序


import itchat
import datetime
import time
import requests


itchat.auto_login(hotReload = True)
users1 = itchat.search_friends(name=u'某人') #查找好友名字
users2 = itchat.search_chatrooms(name=u'某群') #查找好友名字
userName1 = users1[0]['UserName'] #获取名字
userName2 = users2[0]['UserName'] #获取名字

def get_news():
   url = "http://www.weather.com.cn/data/cityinfo/101200101.html" #天气信息来源 101200401孝感
   r = requests.get(url)  #get函数
   r.encoding = 'utf-8' #转码utf-8
   #total = r.json()
   #print(total)


   city = r.json()['weatherinfo']['city']
    # humidity = r.json()['data']['shidu']
    # pm25 = r.json()['data']['pm25']
    # airQuality = r.json()['data']['quality']
    # temperature = r.json()['data']['wendu']
   highTem = r.json()['weatherinfo']['temp2']
   lowTem = r.json()['weatherinfo']['temp1']
   type = r.json()['weatherinfo']['weather']

   #print(city,highTem, lowTem, type)
   return city,highTem,lowTem,type



while 1:

   now = datetime.datetime.now()
   now_str = now.strftime('%Y%M%D %H:%M')[11:]  #时间前11位数
   now_time = time.strftime('%Y-%m-%d %H:%M:%S',time.localtime()) #读取时间
   #print(now)
   print(now_str) #输出计时
   city = str(get_news()[0]) #城市
   highTem = str(get_news()[1])  # 高温
   lowTem = str(get_news()[2])  # 低温
   #now_date = str(get_news()[3])  # 日期
   #week = str(get_news()[4])  # 星期
   #wind = str(get_news()[5])  # 风向
   #windSpeed = str(get_news()[6])  # 风速
   type = str(get_news()[3])  # 天气类型
   #notice = str(get_news()[8])  # 小提示

   if now_str in ['/19 22:59']:   #判断时间点
    itchat.send(now_time+' 早上好!', toUserName=userName1)
    itchat.send(city+' '+lowTem+'~'+highTem+' '+type, toUserName=userName1)
    itchat.send(now_time+' 早上好!', toUserName=userName2)
    itchat.send(city+' '+lowTem+'~'+highTem+' '+type, toUserName=userName2)

    #itchat.send(content, toUserName=userName)
    #itchat.send(editor, toUserName=userName)
    #itchat.send(imag, toUserName=userName)

   time.sleep(55)


if __name__ == '__main__':
        itchat.auto_login()
        itchat.run()

2号程序

import itchat
import datetime
import time
import requests


itchat.auto_login(hotReload = True)
users1 = itchat.search_friends(name=u'XX') #查找好友名字
userName1 = users1[0]['UserName'] #获取名字

def get_news():
    url = "http://t.weather.sojson.com/api/weather/city/101200101" #天气信息来源
    r = requests.get(url)  #get函数
    # r.encoding = 'utf-8' #转码utf-8
    total = r.json()
    city = r.json()['cityInfo']['city']
    # humidity = r.json()['data']['shidu']
    # pm25 = r.json()['data']['pm25']
    # airQuality = r.json()['data']['quality']
    # temperature = r.json()['data']['wendu']
    highTem = r.json()['data']['forecast'][0]['high']
    lowTem = r.json()['data']['forecast'][0]['low']
    now_date = r.json()['data']['forecast'][0]['ymd']
    week = r.json()['data']['forecast'][0]['week']
    wind = r.json()['data']['forecast'][0]['fx']
    windSpeed = r.json()['data']['forecast'][0]['fl']
    type = r.json()['data']['forecast'][0]['type']
    notice = r.json()['data']['forecast'][0]['notice']

    #print(city,highTem, lowTem, now_date, week, wind, windSpeed, type, notice)

    return city,highTem, lowTem, now_date, week, wind, windSpeed, type, notice



while 1:

   now = datetime.datetime.now()
   now_str = now.strftime('%Y%M%D %H:%M')[11:]  #时间前11位数
   now_time = time.strftime('%Y-%m-%d %H:%M:%S',time.localtime()) #读取时间
   #print(now)
   print(now_str) #输出计时
   city = str(get_news()[0]) #城市
   highTem = str(get_news()[1])  # 高温
   lowTem = str(get_news()[2])  # 低温
   now_date = str(get_news()[3])  # 日期
   week = str(get_news()[4])  # 星期
   wind = str(get_news()[5])  # 风向
   windSpeed = str(get_news()[6])  # 风速
   type = str(get_news()[7])  # 天气类型
   notice = str(get_news()[8])  # 小提示

   if now_str in ['/19 07:00']:   #判断时间点
    itchat.send(now_time+' '+week+' 早上好!', toUserName=userName1)
    itchat.send(city+' '+highTem+' '+lowTem+'\n'+type+' '+wind+' '+windSpeed+' '+notice, toUserName=userName1)


   time.sleep(50)


if __name__ == '__main__':
        itchat.auto_login()
        itchat.run()

你可能感兴趣的:(python)