最近女友总是抱怨说我早上都不说想她,女孩子家嘛,矫情一点也是理解的,可是晚上的持续奋斗让早起显得十分无力
于是乎,,有了写一个定时发送早安的程序,再加点天气的报道,岂不是给女神直接拿捏
首先是微信发送消息,利用大神开发的wxauto库,直接实现对微信基本功能的自动化,具体实现可自行研究,这里贴上微信自动发送消息的代码
from wxauto.wxauto import WxUtils
from wxauto import *
if __name__ == '__main__':
wx = WeChat() #获取当前微信客户端
wx.ChatWith('Chris.L') #找到女神的聊天窗口
"""
下面是要发送消息
python中使用'''多行字符串'''来表示多行字符串
使用f'''{带参}'''来对字符串传参
"""
food = '烧包谷'
sendMsg = f'''在吗?
我给你带了你最喜欢的{food}
你男朋友想吃啥?'''
WxUtils.SetClipboard(sendMsg) #将信息复制到粘贴板
wx.SendClipboard() #相当于在聊天框‘ctrl+V’并发送
接下来,,,
没关系,我们还有天气模块,每天定时给女神发天气报道,嘿嘿,小样,浪漫不死你
首先要找到一个可以查询天气的api,左挑右选找到一个比较合适的,url=‘http://t.weather.itboy.net/api/weather/city/’+城市id,比如http://t.weather.itboy.net/api/weather/city/101010100 就可以查询到北京的天气情况,那么显而易见,我们需要获取想查询的城市的id,通过一番摸爬滚打,找到这个网站提供所有城市的id 天气城市代码(带市) - 文档库 (wendangku.net),做到这里,已经确实挺不错了,但是那么多城市,一页一页的翻确实挺麻烦的,于是咱们也是让代码来做这一部分的工作,简单写个爬虫,直接拿捏
直接F12,然后点个下一页,查看网络,很容易发现请求的页码是在url中体现的,于是直接就上代码
import requests #python发送网络请求的
from bs4 import BeautifulSoup #解析html的库,可轻松提取想要的数据
payload = {}
headers = {
'User-Agent': 'PostmanRuntime/7.36.1'
}
if __name__ == '__main__':
for p in range(1, 19): #请求1到19页的数据
url = "https://www.wendangku.net/doc/457378866-"+str(p)+'.html' #页码加载url里
response = requests.request("POST", url, headers=headers, data=payload)
soup = BeautifulSoup(response.text, 'html.parser') #解析html内容并保存在soup
res0 = soup.select('#contents')[0] #提取id为contents的内容
soup = BeautifulSoup(res0.text, 'html.parser')
soup.find_all('<br>', recursive=False) #移除标签'<br>'和'</br>'
soup.find_all('</br>', recursive=False)
res1 = soup.text.split(' ') #根据空格分组
for i in range(0, len(res1),2):
if i+1>=len(res1):
break
print(res1[i]+' '+res1[i+1])
也是很容易就拿到所有的数据,
找到女神所在城市的id,咱直接给写出调用天气的代码
import requests
if __name__ == '__main__':
url = 'http://t.weather.itboy.net/api/weather/city/101310201'
headers = {
'User-Agent': 'PostmanRuntime/7.36.1'
}
response = requests.get(url, headers=headers)
Json = response.json()
print( Json['data']['forecast'][0])
在发送请求的时候有的小伙伴会遇到类似“由于目标计算机积极拒绝,无法连接。”这样的报错,经尝试或许可以用这种方法解决,以下是修改后的代码
if __name__ == '__main__':
url = 'http://t.weather.itboy.net/api/weather/city/101310201'
headers = {
'User-Agent': 'PostmanRuntime/7.36.1'
}
proxies = {"http": None, "https": None}
response = requests.get(url, headers=headers, proxies=proxies)
Json = response.json()
print( Json['data']['forecast'][0])
欧克,到这里大概的代码也是写完了,直接发给女神,让她感受来自程序员的浪漫
最后再为大家整合一下代码,(城市id部分没有整合哦,需要自己取一遍)
import requests
from wxauto.wxauto import WxUtils
from wxauto import *
import datetime
def getWeather():
cityCodes = ['101310201'] #女神所在地的城市id
url = 'http://t.weather.itboy.net/api/weather/city/' + cityCodes[0]
headers = {
'User_Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) '
'Chrome/120.0.0.0 Safari/537.36 Edg/120.0.0.0'
}
proxies = {"http": None, "https": None}
response = requests.get(url, headers=headers, proxies=proxies)
Json = response.json()
return Json['data']['forecast'][0]
weatherf = 0
if __name__ == '__main__':
wx = WeChat()
while 1:
now = datetime.datetime.now()
myTime = now.time().hour # 小时数
if myTime > 9:
weatherf = 0
if weatherf == 0 and myTime == 9: #9点发消息
weather = getWeather()
wx.ChatWith('Hallew,Worrd')
sendMsg = f'''女神你好~
现在是你的男朋友为您报道三亚的天气情况
今天是{weather['date']}号
最{weather['high']}
最{weather['low']}
天气是{weather['type']}
{weather['notice']}
世界上最遥远的距离 ,是我在 if 里你在 else 里,似乎一直相伴又永远分离;
世界上最痴心的等待 ,是我当 case 你是 switch ,或许永远都选不上自己;
世界上最真情的相依 ,是你在 try 我在 catch 。无论你怎么发小脾气,我都默默承受,静静处理。然后,再来期待我们的 finally
'''
WxUtils.SetClipboard(sendMsg)
wx.SendClipboard()
weatherf = 1 #一天只发一次