python爬取天气预报用163邮箱发
不BB,直接上代码,导完包,改完邮箱就能跑。
import smtplib
import pandas as pd
import numpy as np
import requests
from lxml import etree
from email.mime.text import MIMETextdef parse(url = 'https://www.tianqi.com/shanghai'):headers = {'User-Agent':'Mozila/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.87 Safari/537.36'}html = requests.get(url,headers = headers)bs = etree.HTML(html.text)#今天天气相关数据:日期,星期几,天气,最低气温,最高气温today_date = bs.xpath('//ul[@class = "week"]/li[1]/b/text()')[0]today_week = bs.xpath('//ul[@class = "week"]/li[1]/span/text()')[0]today_weather = bs.xpath('//ul[@class = "txt txt2"]/li[1]/text()')[0]today_low = bs.xpath('//div[@class = "zxt_shuju"]/ul/li[1]/b/text()')[0]today_high = bs.xpath('//div[@class = "zxt_shuju"]/ul/li[1]/span/text()')[0]#明天天气相关数据,维度和上述一致tomorrow_date = bs.xpath('//ul[@class = "week"]/li[2]/b/text()')[0]tomorrow_week = bs.xpath('//ul[@class = "week"]/li[2]/span/text()')[0]tomorrow_weather = bs.xpath('//ul[@class = "txt txt2"]/li[2]/text()')[0]tomorrow_low = bs.xpath('//div[@class = "zxt_shuju"]/ul/li[2]/b/text()')[0]tomorrow_high = bs.xpath('//div[@class = "zxt_shuju"]/ul/li[2]/span/text()')[0]tomorrow = ('明天是%s,%s,%s,%s-%s度,温差%d度')% \(tomorrow_date,tomorrow_week,tomorrow_weather,tomorrow_low,tomorrow_high,int(int(tomorrow_high)-int(tomorrow_low)))print(('明天是%s,%s,%s,%s-%s度,温差%d度')% \(tomorrow_date,tomorrow_week,tomorrow_weather,tomorrow_low,tomorrow_high,int(int(tomorrow_high)-int(tomorrow_low))))#计算今明两天温度差异,这里用的是最高温度temperature_distance = int(tomorrow_high) - int(today_high)if temperature_distance > 0:a = '明日升温%d' % temperature_distanceprint('明日升温%d' % temperature_distance)if temperature_distance < 0:a = '明日降温%d' % temperature_distanceprint('明日降温%d' % temperature_distance)else:a = '最高气温不变'print('最高气温不变')content = tomorrow,areturn contentweather = parse()# 第三方 SMTP 服务
mail_host = "smtp.163.com" # SMTP服务器
mail_user = "1*****@163.com" # 用户名
mail_pass = "123456" # 密码sender = '1****@163.com' # 发件人邮箱
receivers = ['1*****@163.com'] # 接收邮件,可设置为你的其他邮箱,如果是QQ邮箱,需要开启stmp##############使用qq邮箱的时候,记得要去开启你的qq邮箱的smtp服务;############### 方法:# 1)登录到你的qq邮箱;# 2)找到首页顶部的【设置】并点击;# 3)找到【账户】这个选项卡并点击,然后在页面中找到“SMTP”相关字样,找到【开启】的超链接,点击后会告诉你开启方法(需要发个短信),然后按照指示操作,最终会给你一个密码,这个密码可以用于在代码中当作邮箱密码# ###########################################################################sss = ''.join(weather)#将 tuple 转换为string
title = 'Python SMTP Mail Test' # 邮件主题
message = MIMEText(sss, 'plain', 'utf-8') # 内容, 格式, 编码
message['From'] = "{}".format(sender)
message['To'] = ",".join(receivers)
message['Subject'] = titletry:smtpObj = smtplib.SMTP_SSL(mail_host, 465) # 启用SSL发信, 端口一般是465smtpObj.login(mail_user, mail_pass) # 登录验证smtpObj.sendmail(sender, receivers, message.as_string()) # 发送print("mail has been send successfully.")
except smtplib.SMTPException as e:print(e)
python爬取天气预报用163邮箱发相关推荐
- python爬取天气预报并发送短信_Python3爬虫教程之利用Python实现发送天气预报邮件...
前言 此次的目标是爬取指定城市的天气预报信息,然后再用Python发送邮件到指定的邮箱. 下面话不多说了,来一起看看详细的实现过程吧 一.爬取天气预报 1.首先是爬取天气预报的信息,用的网站是中国天气 ...
- python爬取天气预报数据,并实现数据可视化
文章目录 一.前言 二.爬取目标及结果展示 三.页面分析 四.完整代码 五.补充 六.不足(经读者反馈) 一.前言 在爬取数据时,有些数据,如图片.视频等等,爬到就是赚到:而有时候,我们爬到的可能只是 ...
- python爬取天气预报源代码_python抓取天气并分析 实例源码
[实例简介] Python代码抓取获取天气预报信息源码讲解.这是一个用Python编写抓取天气预报的代码示例,用python写天气查询软件程序很简单.这段代码可以获取当地的天气和.任意城市的天气预报, ...
- 利用python爬取天气预报_python实现天气爬虫——利用xpath爬取七天天气预报数据...
python实现天气爬虫--利用xpath爬取七天天气预报数据 import pandas as pd import lxml import requests import csv from lxml ...
- python爬取天气预报数据并保存为txt格式_今天分享一个用Python来爬取小说的小脚本!(附源码)...
本文的文字及图片来源于网络,仅供学习.交流使用,不具有任何商业用途,如有问题请及时联系我们以作处理. 以下文章天气预报数据分析与统计之美 ,作者:❦大头雪糕❦ Python GUI制作小说下载器教学讲 ...
- python爬取天气预报_分分钟学会用Python爬取天气预报
###第一步: 请确保你已经安装了Requests和Beautifulsoup4的库,否则你可以打开CMD(命令提示符)然后输入 pip3 install requests pip3 install ...
- 分分钟学会用Python爬取天气预报
我假设你已经符合上述的标准,现在我们就来开始第一个爬虫的网站,我们首先挑选一个下手:附上URL:中国天气网(http://www.weather.com.cn/weather1d/101280101. ...
- Python 爬取 百度地图搜索结果
Python 爬取 百度地图搜索结果 第一次发CSDN = = 其实是因为以前实在没实现过什么像样的东西 emmm *就当是学习记录吧~ 还有很多不足,希望大家能在评论区指正呀 大二的小白会继续努力的 ...
- Python爬取糗事百科段子+定时发送QQ邮箱
文章目录 前言 1. 库导入及介绍 2. 获取网页源码 3. 提取需要的信息 4. 优化输出数据 5. 发送邮件 6. 实现定时发送 7. 源码 前言 学习Python爬虫也有段时间了,总想着搞点事做 ...
最新文章
- 苹果为了不让AirTag被用来跟踪,将推出一个安卓应用
- 【白话机器学习】算法理论+实战之K近邻算法
- Spring 注入内部 Beans
- [Swift]检查API可用性
- jQuery form插件使用详解
- css 动画类库Animate.css
- Hive_Hive的数据模型_分区表
- 左侧固定 右侧自适应
- 使用 Python SimpleHTTPServer 快速共享文件
- oracle 的逻辑结构设计,浅谈Oracle数据库逻辑结构
- mt4交易系统源码_如何将源码加载到mt4里面
- python简单爬虫代码
- IEC 61850通信协议体系介绍和分析
- C语言课程设计 简单的单词学习系统
- 使用selenium爬取搜狗微信文章
- Halcon生成标定描述文件
- win10共享打印机搜索不到计算机,Win10专业版在局域网中搜索不到打印机
- 试算平衡表示例图_期末试算平衡表截图
- 人脸识别广告显示屏通过人像识别、面部识别应用
- 谷歌浏览器 翻译无法使用的问题