#coding=gbk
import pyautogui
import pyperclip
import time
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
def get_msg(): #内容输入函数
options = Options()
options.add_argument(
'user-agent=Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.96 Safari/537.36')
options.add_argument('--no-sandbox')
options.add_argument('disable-infobars')
options.add_experimental_option("excludeSwitches", ["ignore-certificate-errors"])
options.add_experimental_option('excludeSwitches', ['enable-logging'])
browser = webdriver.Chrome(options=options)
browser.implicitly_wait(60) # 60秒
browser.maximize_window()
weather_url = 'http://www.weather.com.cn/weather1d/101121201.shtml#input'
browser.get(weather_url)
time.sleep(2)
morning = browser.find_element(By.XPATH, "//*[@id='today']/div[1]/ul/li[1]/h1").text
morning_temperatrue = browser.find_element(By.XPATH, "//*[@id='today']/div[1]/ul/li[1]/p[2]/span").text
night_temperatrue = browser.find_element(By.XPATH, "//*[@id='today']/div[1]/ul/li[2]/p[2]/span").text
night = browser.find_element(By.XPATH, "//*[@id='today']/div[1]/ul/li[2]/h1").text
morning_weather = browser.find_element(By.XPATH, "//*[@id='today']/div[1]/ul/li[1]/p[1]").text
night_weather = browser.find_element(By.XPATH, "//*[@id='today']/div[1]/ul/li[2]/p[1]").text
print(morning + " " + morning_weather + " " + morning_temperatrue + "°C")
print(night + " " + night_weather + " " + night_temperatrue + "°C")
browser.close()
return morning + " " + morning_weather + " " + morning_temperatrue + "°C" +"\r\n"+night + " " + night_weather + " " + night_temperatrue + "°C"
def send(msg):
pyperclip.copy(msg) # 复制需要发送的内容到粘贴板
pyautogui.hotkey('ctrl', 'v') # 模拟键盘 ctrl + v 粘贴内容
pyautogui.press('enter') # 发送消息
def send_msg(friend):
pyautogui.hotkey('ctrl', 'alt', 'w') # Ctrl + alt + w 打开微信
pyautogui.hotkey('ctrl', 'f') # 搜索好友
pyperclip.copy(friend) # 复制好友昵称到粘贴板
pyautogui.hotkey('ctrl', 'v') # 模拟键盘 ctrl + v 粘贴
time.sleep(1)
pyautogui.press('enter') # 回车进入好友消息界面
# 一条一条发送消息
while(1):
send(get_msg())
time.sleep(20) # 每条消息间隔 1 秒
if __name__ == '__main__':
friend_name = "文件传输助手" #对方用户名称:与微信备注保持一致,尽量使用英文
send_msg(friend_name)
python抓取天气数据,发送到微信
最新推荐文章于 2024-08-21 16:56:45 发布
![](https://img-home.csdnimg.cn/images/20240711042549.png)