栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

还想着过单身节?这不赶快写个微信实时天气发送给中意人~

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

还想着过单身节?这不赶快写个微信实时天气发送给中意人~

每周爬虫——获取每日天气并及时传送给你在意的人 1、前言

刚刚学习爬虫不久,恰好又赶上冬天的冷空气,让我感觉到我的用武之地!望各位兄弟抬举在下,通过个人手动亲测实现并且结合网上资料完成小练习。

2、前期准备

1、需求设定:

这是爬虫学习的一次实践,这里我以微信为载体实现每日的傻瓜式发送消息等功能,并且能够爬取网站上的天气信息。并且实现每日发送。

基于如上,我们需要准备以下内容:

  • itchat
  • 爬虫
3、爬虫

我们使用BeautifulSoup和urllib来完成爬虫部分的创建。

3.1 下载

方式一:如果你使用pycharm进行编程,那么可以参照我的导入进行安装即可,或者在pycharm终端进行相应下载。

from urllib.request import urlopen
from bs4 import BeautifulSoup
import lxml
import requests

比如你如果哪个模块没有安装,pycharm可以帮你调入到终端进行安装。当然名字也要匹配。

方式二:pip自动安装

1、解析器安装

pip install lxml

解析器的优势自然在于速度快,能够加大对于解析文档的容错率。

2、安装beautifulsoup4

pip3 install beautifulsoup4

因为最新版本是4.X,就亲测有效吧

3.安装urllib

pip install urllib

顾名思义,你可以理解成找url网址的一个工具。

3.2 具体步骤

1、首先找到你要爬取的网站,获取URL地址值

#1、找到对应的网站进行爬取
html = urlopen("http://tianqi.sogou.com/pc/weather/2332588")
#2、获取htmlURL
bs0bj = BeautifulSoup(html,'html.parser')
bs0bj1 = BeautifulSoup(html1,'html.parser')

2、根据find函数或者find_all函数获取标签值

#3、通过find函数找到相应的内容
    date = bs0bj.find("a",attrs={"class":"date"})
    temperature =bs0bj.find("div",attrs={"class":"r1-tmp"})
    air_humidity= bs0bj.find("span",attrs={"class":"hundity"})

    air_quality = bs0bj1.find("span",attrs={"class":"temp"})

稍微对比一下:

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传

比如在第18行中,我们先找到需要的标签,找到其标签的内容,然后后面寻找对应的类,即temp。最后返回字符内容。

当然也可以使用findall,这种对于同一个标签和class类一致的内容找到直接放置于一个列表中,当然返回的是一个列表list。

稍微区分两者的区别即可。

3.3 拼接信息

稍微做一个字符串拼接即可。

#4、将天气信息保存至列表中
    weather=[]
    weather.append("今日时间:"+date.get_text().replace(" ","").replace("n",""))
    weather.append("气温:"+temperature.get_text().replace("n",""))
    weather.append(air_humidity.get_text())
    weather.append("空气质量:"+air_quality.get_text())
#返回weather
#main函数
return weather
if __name__ == '__main__':
    weather = getMessage()
    print(weather)
4、itchat

完成前面的爬虫,但是我们要将收集到的信息传给(我的)女朋友,那么此时 itchat是一个非常方便简单的python的微信接口。我们根据需求,能够完成微信登录或者登出,并且能够发送消息即可。

4.1 pip下载
pip install itchat-uos

完成下载后,可以进行小小的尝试喔。

这里记住,由于最新的微信网页版不支持itchat登录微信网页版,这里需要下载的是itchat-uos。

即如下展示,确定我们使用的是itchat-uos,就不会出现后续代码块的报错。

一般的报错提示:
1203为了你的帐号安全,此微信号不能登录网页微信。你可以使用Windows微信或Mac微信在电脑端登录。Windows微信下载地址:https://pc.weixin.qq.com Mac微信下载地址:https://mac.weixin.qq.com 
4.2 编写内容

先写完爬虫的部分再完成这个内容的编写喔

#1、获取main中的weather返回值
    weatherInformation = getMessage()
#2、获取weatherInfo中各个列表的返回值,注意异常处理
    Date = weatherInformation[0]
    temperature = weatherInformation[1]
    air_humidity = weatherInformation[2]
    air_quality = weatherInformation[3]
    bless="今天也是元气满满的一天哇~~"
    message ="早上好,xx!"+"n"+
             Date+"n"+
             temperature+"n"+
             air_quality+"n"+
             air_humidity+"n"+
             bless
    print(message)
except:
    message="今天也是开心的一天喔~~"

#3、微信登录以及推出
ic.auto_login(hotReload=True)

users = ic.search_friends(name='备注名')
userName = users[0]['UserName']
ret = ic.send(msg=message, toUserName=userName)

if ret:
    print("成功发送")
else:
    print("失败发送")

time.sleep(60)
ic.logout()   

后面就可以再创建一个定时的内容。

4.3 定时器
import time
def starttime():
    while(1):
        #1、format格式
        Hour = int(time.strftime("%H"))
        print(Hour)
        if Hour==19 :
            print("倒计时一小时!")
            time.sleep(60)
        if Hour==20:
            print("是时候啦!")
            break
        else:
            print("还不是时候喔!")
            time.sleep(3500)
if __name__ == "__main__":
     starttime()

就是获取小时然后选择一个时间定时发送信息即可。

5、总结

再附上信息爬虫的全部内容,加上定时器和微信内容发送共三个文件。

from urllib.request import urlopen
from bs4 import BeautifulSoup
import lxml
import requests

def getMessage():
    #1、找到对应的网站进行爬取
    html = urlopen("http://tianqi.sogou.com/pc/weather/2332588")
    html1 = urlopen("http://tianqi.sogou.com/pc/quality/2332588")
    #2、获取htmlURL
    bs0bj = BeautifulSoup(html,'html.parser')
    bs0bj1 = BeautifulSoup(html1,'html.parser')
    #3、通过find函数找到相应的内容
    date = bs0bj.find("a",attrs={"class":"date"})
    temperature =bs0bj.find("div",attrs={"class":"r1-tmp"})
    air_humidity= bs0bj.find("span",attrs={"class":"hundity"})

    air_quality = bs0bj1.find("span",attrs={"class":"temp"})


    #4、将天气信息保存至列表中
    weather=[]
    weather.append("今日时间:"+date.get_text().replace(" ","").replace("n",""))
    weather.append("气温:"+temperature.get_text().replace("n",""))
    weather.append(air_humidity.get_text().replace("n",""))
    weather.append("空气质量:"+air_quality.get_text())
    return weather
if __name__ == '__main__':
    weather = getMessage()
    print(weather)

最终的展示如下:


当然这个也需要后续配置到服务器或者手动按时运行,才能够完成按时发送内容喔。
总之就是我的个人小练习,谢谢大家的阅读。在此祝大家能够早日终成眷属喔嘻嘻

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/487736.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号