python爬取天气情况

下面为示例代码:

from urllib.request import urlopen
from bs4 import BeautifulSoup
from urllib.error import HTTPError
import smtplib
from email.mime.text import MIMEText
from email.utils import formataddr
import time """
爬虫程序是一个需要后期投入很大维护力度的,就比如网页开发者将来在某一时间重构了html代码,这就很有可能导致爬虫程序的失败,所以写爬虫程序要尽量做到未雨绸缪,让爬虫程序更健壮,这样你才能睡一个安稳的觉
这个程序很简单,大体分为两个部分,爬虫部分和邮件部分,代码还有很大的优化空间,比如一些常量可以拿到外面,这样代码看起来会更整洁,邮件内容是以html格式的形式发送的,这样你就可以改成自己喜欢的样式。
我不建议你频繁的去发邮件,邮箱可能会被封掉,信件退回,导致发送失败
发送电子邮件的邮箱要开启smtp客户端功能,邮箱——>设置——>开启SMTP服务,获取授权码(就是程序里需要登陆邮箱的密码)
"""
#获取当天的天气情况
def get_weather(url):
try:
html=urlopen(url).read()
except HTTPError as e:
return None
try:
weather_list=[]
bs0bj=BeautifulSoup(html,"html.parser")
time.sleep(5)
weather=bs0bj.find("div",{"class":"condition-icon wx-weather-icon vector"}).next_siblings
title=bs0bj.body.h1.get_text()
weather_list.append(title)
for next in weather:
weather_list.append(next.get_text())
except AttributeError as e:
return None
return weather_list

#获取未来5天的天气情况
def get_5weathers(url):
try:
html=urlopen(url).read()
except HTTPError as e:
return None
try:
weather5_list=[]
bs0bj=BeautifulSoup(html,"html.parser")
weathers=bs0bj.find("table",{"class":"twc-table"}).tbody
for child in weathers.children:
list1=[]
for i in child.children:
list1.append(i.get_text())
list1.remove("")
weather5_list.append(list1)
except AttributeError as e:
return None
return weather5_list

#等到的数据形如一下数据格式
# weather=['北京, 中国', '3°', '晴朗', '体感温度 -1°', '高温 -- 低温 -7°紫外线指数 0(最大值10)']
# weathers=[['今天晚上\n12月 18日', '大部晴朗', '---7°', '0%', '北 15 公里 /小时 ', '40%'], ['星期二12月 19日', '晴朗', '5°-5°', '0%', '西南 21 公里 /小时 ', '32%'], ['星期三12月 20日', '晴朗', '7°-6°', '0%', '西北 22 公里 /小时 ', '33%'], ['星期四12月 21日', '晴朗', '6°-6°', '0%', '西南西 11 公里 /小时 ', '41%'], ['星期五12月 22日', '晴朗', '8°-6°', '0%', '北 16 公里 /小时 ', '30%'], ['星期六12月 23日', '晴朗', '8°-3°', '0%', '西北西 14 公里 /小时 ', '29%']]

# #***********************发送电子邮件*******************************
#第三方SMTP服务器 def sendEmail():
msg=MIMEText(mail_msg,"html","utf-8")
msg["From"]=formataddr(["裤裆人",mail_user])
msg["To"]=formataddr(["小裤裆人s",receive_address])
msg["Subject"]="北京天气预报"
try:
smtp0bj=smtplib.SMTP_SSL(mail_host,465)
smtp0bj.login(mail_user,mail_pass)
smtp0bj.sendmail(mail_user,receive_address,msg.as_string())
smtp0bj.quit()
print("mail has been send to %s successfully."%receive_address)
except smtplib.SMTPException as e:
print(e) if __name__=="__main__":
weather=get_weather("http://www.weather.com")
weathers=get_5weathers("https://weather.com/zh-CN/weather/5day/l/CHXX0008:1:CH")
if weather==None:
exit()
if weathers==None:
exit()
mail_host="smtp.163.com"
mail_user="150XXxX65@163.com" #发件人邮箱账号
mail_pass="jXXxX199XXxX" #发件人邮箱密码
receives=[
"4352XXxX@qq.com",
"3426XXxX@qq.com",
"5437XXxX2@qq.com",
"6353XXxX9@qq.com",
] #收件人邮箱账号
mail_msg="""
<h1>裤裆人天气预报</h1>
<p style="color:#99cc00;font-size:15px">%s-->目前天气状况:温度%s ,%s ,%s ,%s </p>
<h3>以下是未来5天的天气情况</h3>
<table width="800px" border="0" cellspacing="1" cellpadding="0" style="text-align:center">
<thead style="background-color:#3399ff">
<tr style="line-height:40px;">
<th>白天</th>
<th>说明</th>
<th>高/低</th>
<th>降雨概率</th>
<th>风力</th>
<th>湿度</th>
</tr>
</thead>
<tbody>
<tr style="background: #ccffcc">
<td>%s</td>
<td>%s</td>
<td>%s</td>
<td>%s</td>
<td>%s</td>
<td>%s</td>
</tr>
<tr style="background: #ccffcc">
<td>%s</td>
<td>%s</td>
<td>%s</td>
<td>%s</td>
<td>%s</td>
<td>%s</td>
</tr>
<tr style="background: #ccffcc">
<td>%s</td>
<td>%s</td>
<td>%s</td>
<td>%s</td>
<td>%s</td>
<td>%s</td>
</tr>
<tr style="background: #ccffcc">
<td>%s</td>
<td>%s</td>
<td>%s</td>
<td>%s</td>
<td>%s</td>
<td>%s</td>
</tr>
<tr style="background: #ccffcc">
<td>%s</td>
<td>%s</td>
<td>%s</td>
<td>%s</td>
<td>%s</td>
<td>%s</td>
</tr>
</tbody>
</table>
<p style="color:red;font-size:10px">注意:每天早上八点准时发送邮件,希望小伙伴们,多多关注天气情况,注意保暖!</p> """%(weather[0],weather[1],weather[2],weather[3],weather[4],weathers[0][0],weathers[0][1],weathers[0][2],weathers[0][3],weathers[0][4],weathers[0][5],weathers[1][0],weathers[1][1],weathers[1][2],weathers[1][3],weathers[1][4],weathers[1][5],weathers[2][0],weathers[2][1],weathers[2][2],weathers[2][3],weathers[2][4],weathers[2][5],weathers[3][0],weathers[3][1],weathers[3][2],weathers[3][3],weathers[3][4],weathers[3][5],weathers[4][0],weathers[4][1],weathers[4][2],weathers[4][3],weathers[4][4],weathers[4][5])
for receive_address in receives:
sendEmail()
time.sleep(120)
exit()

python爬虫 发送定时气象预报的更多相关文章

  1. Python爬虫之定时抢购淘宝商品

    Python爬虫之定时抢购淘宝商品 import time from selenium import webdriver import datetime class Spider: def __ini ...

  2. python 爬虫 发送每天天气

    #!/usr/bin/python# -*- coding: UTF-8 -*-import requests,bs4,smtplib,sysimport smtplib, sysfrom email ...

  3. Python下发送定时消息给微信好友

    """ Description:时间可以改长一点 一分钟一个 Author:Nod Date: Record: #---------------------------- ...

  4. python 爬虫每天定时启动爬虫任务

     # coding=utf-8 import datetime import time def doSth(): # 这里是执行爬虫的main程序     print '爬虫要开始运转了....'   ...

  5. Python 爬虫实战(一):使用 requests 和 BeautifulSoup

    Python 基础 我之前写的<Python 3 极简教程.pdf>,适合有点编程基础的快速入门,通过该系列文章学习,能够独立完成接口的编写,写写小东西没问题. requests requ ...

  6. 又面试了Python爬虫工程师,碰到这么几道面试题,Python面试题No9

    第1题:动态加载又对及时性要求很高怎么处理? 如何知道一个网站是动态加载的数据? 用火狐或者谷歌浏览器 打开你网页,右键查看页面源代码,ctrl +F 查询输入内容,源代码里面并没有这个值,说明是动态 ...

  7. python爬虫---详解爬虫分类,HTTP和HTTPS的区别,证书加密,反爬机制和反反爬策略,requests模块的使用,常见的问题

    python爬虫---详解爬虫分类,HTTP和HTTPS的区别,证书加密,反爬机制和反反爬策略,requests模块的使用,常见的问题 一丶爬虫概述       通过编写程序'模拟浏览器'上网,然后通 ...

  8. 教你用python爬虫监控教务系统,查成绩快人一步!

    教你用python爬虫监控教务系统,查成绩快人一步!这几天考了大大小小几门课,教务系统又没有成绩通知功能,为了急切想知道自己挂了多少门,于是我写下这个脚本. 设计思路:设计思路很简单,首先对已有的成绩 ...

  9. Python 爬虫模拟登陆知乎

    在之前写过一篇使用python爬虫爬取电影天堂资源的博客,重点是如何解析页面和提高爬虫的效率.由于电影天堂上的资源获取权限是所有人都一样的,所以不需要进行登录验证操作,写完那篇文章后又花了些时间研究了 ...

随机推荐

  1. JavaScript权威指南--WEB浏览器中的javascript

    知识要点 1.客户端javascript window对象是所有客户端javascript特性和API的主要接入点.它表示web浏览器的一个窗口或窗体,并且可以用window表示来引用它.window ...

  2. windows下的IO模型之事件选择(WSAEventSelect)模型

    异步选择模型类似的是,它也允许应用程序在一个或多个套接字上,接收以事件为基础的网络事件通知.对于异步选择模型采用的网络事件来说,它们均可原封不动地移植到事件选择模型.事件选择模型和异步选择模型最主要的 ...

  3. UVA-11419 SAM I AM (最小点覆盖)

    题目大意:在一个n*m的网格中,有k个目标,现在可以任选一行或列消除在其上的所有目标,求出最少选择次数及选法. 题目分析:经典的最小点覆盖问题,并且输出一个最小点覆盖集.在求出最大匹配之后,以未覆盖的 ...

  4. [转]Ubuntu默认root用户密码

    Ubuntu默认root用户密码 在试验的过程中,安装完Ubuntu后忽然意识到没有设置root密码,不知道密码自然就无法进入根用户下. 到网上搜了一下,原来是这么回事:Ubuntu的默认root密码 ...

  5. 143. Long Live the Queen 树形dp 难度:0

    143. Long Live the Queen time limit per test: 0.25 sec. memory limit per test: 4096 KB The Queen of ...

  6. 为什么不建议将 font-size 设置为 12px 以下?如果一定要设置为 12px 以下要怎么做?

    问题:为什么不建议将 font-size 设置为 12px 以下?如果一定要设置为 12px 以下要怎么做? 先看看把 font-size 设置为 12px 以下时的效果:(浏览器为 Chrome 5 ...

  7. Java LRU的实现

    最近在leetcode上做题的时,看到了一道有关LRU Cache的题目,正好我当初面试阿里巴巴的时候问到的.主要采用linkedHashMap来实现. package edu.test.algori ...

  8. 面筋: 奇虎360 c++ 后台开发 实习生 面试

    投的是360上海的商业化部门,岗位是C++服务端开发实习生,记录一下面试历程: 视频面试,但是是有代码框让你写代码的. 一面: Q:先说一下个人信息,做过的项目 A:.......... Q:先写个翻 ...

  9. Beta阶段第1周/共2周 Scrum立会报告+燃尽图 01

    作业要求[https://edu.cnblogs.com/campus/nenu/2018fall/homework/2383] 版本控制:https://git.coding.net/liuyy08 ...

  10. L181 The microscopic structure of a cat’s tongue helps keep its fur clean

    T.S. eliot's mystery cat, Macavity, besides being a criminal mastermind able to evade the combined r ...