每天早上在QQ上给女朋友发今天的天气情况,以及早安祝福,代码如下:
import pyautogui
import chardet
import pyperclip
import win32api
import time
import os
import random
import requests
from bs4 import BeautifulSoup
def Getweather():
# print('请输入城市名称:')
# cityname=input()
cityname = "常州"
host = 'http://api.k780.com:88/?app=weather.future&weaid=%s'%cityname
url = host+'&appkey=10003&sign=b59bc3ef6191eb9f747dd4e83c99f2a4&format=xml'
response = requests.get(url)
content = response.text
soup = BeautifulSoup(content, 'lxml')
#日期
day = []
target = soup.find_all('days')
for each in target:
day.append(each.text)
#星期几
week = []
target = soup.find_all('week')
for each in target:
week.append(each.text)
#城市
city = []
target = soup.find_all('citynm')
for each in target:
city.append(each.text)
#温度
temperature = []
target = soup.find_all('temperature')
for each in target:
temperature.append(each.text)
#天气状况
weather = []
target = soup.find_all('weather')
for each in target:
weather.append(each.text)
#风向
wind = []
target = soup.find_all('wind')
for each in target:
wind.append(each.text)
#风力
winp = []
target = soup.find_all('winp')
for each in target:
winp.append(each.text)
length = len(day)
listAll = [0] * length
for i in range(length):
listAll[i] = [city[i], day[i], week[i], temperature[i], weather[i], wind[i], winp[i]]
return listAll
def check_code(text):
adchar = chardet.detect(text)
if adchar['encoding'] == 'gbk' or adchar['encoding'] == 'GBK' or adchar['encoding'] == 'GB2312':
true_text = text.decode('GB2312', "ignore")
else:
true_text = text.decode('utf-8', "ignore")
return true_text
def open_text(text):
with open(text + '.txt', 'rb')as f:
file_text = f.read()
file_text2 = check_code(file_text)
return file_text2
def open_app():
win32api.ShellExecute(0, 'open', r'C:\Program Files (x86)\Tencent\QQ\Bin\QQ.exe', '','',1)
time.sleep(3)
pyautogui.hotkey('enter', interval=0.1)
time.sleep(5)
def send_text():
wheather = Getweather()
#点击输入框
#pyautogui.click(x=2700, y=470, interval=0.1)
message = open_text('message').split('\n')
i = random.randint(0, 40)
weather = wheather[0][0] + ',' + wheather[0][1] + ',' + wheather[0][2] + ',' + wheather[0][3] + ',' + wheather[0][4] + ',' + wheather[0][5]
pyperclip.copy(weather)
pyautogui.hotkey('ctrl', 'v', interval=0.1)
pyautogui.hotkey('enter', interval=0.1)
pyperclip.copy(message[i])
pyautogui.hotkey('ctrl', 'v', interval=0.1)
pyautogui.hotkey('enter', interval=0.1)
def find_all():
time.sleep(1)
nameid = open_text('name').split('\n')
for id in nameid:
# 2400,470 qq/tim搜索位置
pyautogui.click(x=2560, y=760, interval=0.1)
pyperclip.copy(id)
pyautogui.hotkey('ctrl', 'v', interval=0.1)
pyautogui.doubleClick(x=2560, y=900, interval=0.1)
# 发送消息
send_text()
if __name__ == '__main__':
open_app()
#窗口最大化
#pyautogui.hotkey('alt',' ','x', interval=0.1)
find_all()
os.system("taskkill /F /IM QQ.exe")