每天定时用爬虫爬取银川天气通过微信发送给宝贝

PC下写一个.py文件,import的模块有re、requests、bs4、wxpy等相关模块。运行脚本时弹出二维码,通过手机微信扫描登录,搜索相关朋友的备注名称,设定每天发送信息的时间。发送天气信息。

当报错:

import requests ModuleNotFoundError: No module named ‘requests’
缺少导入的requests库。

解决办法:

开始菜单选择运行,输入cmd运行,然后cd命令进入到python安装目录下的Scripts文件中,

然后输入pip install requests,就好了。

import re
import requests
from requests import exceptions
from urllib.request import urlopen
from bs4 import BeautifulSoup
from wxpy import *
import time
import datetime
from threading import Timer
import  schedule

bot=Bot() #登陆微信,会自动弹出二维码

def sendblogmsg(content):
	my_group = bot.friends().search(u'宝贝')[0] #搜索好友备注名称
	my_group.send(content) ##发送相关的信息

def job():
    #城市的url,
    resp=urlopen('http://www.weather.com.cn/weather/101170101.shtml')
    soup=BeautifulSoup(resp,'html.parser')
    
    tagDate=soup.find('ul', class_="t clearfix")
    dates=tagDate.h1.string
    
    dates=soup.find('input',id="hidden_title")['value'][10:]
    #获取温度:
    #tem=soup.find('p',class_="tem").span.string
    tem=soup.find('p',class_="tem").i.string
    
    #获取天气:
    weather=soup.find('p',class_="wea").string
    #风力等级:
    #win = soup.find('p',class_="win").find('span').string
    win = soup.find('p',class_="win").i.string  #风力
    win1=soup.find('p',class_="win").span['title'] #风向
    #还可以获取其他的:
    
    
    now_time = str(datetime.datetime.now())[:19] #当前时间
    ##结合这些信息:
    contents = '北京时间:' +now_time+'  '+dates+'\n' + '银川天气:'+weather + '\n'+'温度:'+tem+'\n'+\
    '风向:'+win1+'\n'+'风力:'+win+'\n'+'温馨提示小仙女!'
    sendblogmsg(contents)
    #设置发送时间间隔,时间为秒
    #t = Timer(60*60, job)
    #t.start()
 
if __name__ == "__main__":
   #定时:
    schedule.every().day.at("14:41").do(job) #每天08:00执行job()函数
    while True:
        schedule.run_pending()#确保schedule一直运行
        time.sleep(1)
    bot.join()

修改:day.at(“14:41”)中的时间:
运行后等待运行结果如图:
在这里插入图片描述

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值