邮箱设置
在使用Python自动发送邮件之前,需要对我们的QQ邮箱进行简单的配置,过程如下:
1.首先登陆QQ邮箱,选择“账户”如下图所示:
2.在账户页面往下拉,看到“POP3/SMTP”设置,点击开启按钮,如下图所示:
3.弹出如下图所示界面,然后发送这条短信至指定号码,点击“我已发送”按钮。
4.弹出的提示中会显示16位授权码,注意一定要记住这个授权码,后面写Python代码也需要,然后点击“确定”按钮。
5.接下来将收取选项设置为“全部”,并点击“保存”按钮即可。注意端口号如下:
爬取天气预报
1.分析网页
中国天气网: http://www.weather.com.cn/weather1d/101280101.shtml
2.分析源码,获取你需要的信息,我这里获取第二条的天气情况
话不多说,直接上代码
spiders
# -*- coding: utf-8 -*-
import scrapy
from bs4 import BeautifulSoup
class Weatopost01Spider(scrapy.Spider):
name = 'weaToPost01'
allowed_domains = ['com.cn']
start_urls = ['http://www.weather.com.cn/weather1d/101280101.shtml']
def parse(self, response):
soup = BeautifulSoup(response.text,"html.parser")
content = ""
name = soup.find_all(attrs={"class":"t"})[0]
mingtian = name.find_all('li')[0]
wea = mingtian.find(attrs={"class": "wea"}).get_text()
tem = mingtian.find(attrs={"class": "tem"}).get_text()
yield {"天气": wea, "温度": tem.replace("\n", "")}
pipelines
# -*- coding: utf-8 -*-
# Define your item pipelines here
#
# Don't forget to add your pipeline to the ITEM_PIPELINES setting
# See: https://docs.scrapy.org/en/latest/topics/item-pipeline.html
import smtplib
from email.mime.text import MIMEText
from email.header import Header
class WeatopostPipeline(object):
def process_item(self, item, spider):
msg_from = "邮箱账号"
EMAIL_HOST_PASSWORD = '授权码'
msg_to = "目标邮箱账号"
subject = "天气预报"
wea = item['天气']
tem = item['温度']
msgend = "广州明天天气为{},温度为{},此消息来自最可爱的小猪猪".format(wea,tem)
msg = MIMEText(msgend, 'plain', 'utf-8')
msg['Subject'] = subject
msg['From'] = msg_from
msg['To'] = msg_to
try:
s = smtplib.SMTP_SSL("smtp.qq.com", 465)
s.set_debuglevel(1)
s.login(msg_from, EMAIL_HOST_PASSWORD)
s.sendmail(msg_from, msg_to, msg.as_string())
print("发送成功")
except :
print("发送失败")
finally:
s.quit()
settings
#使用管道
ITEM_PIPELINES = {
'weaToPost.pipelines.WeatopostPipeline': 300,
}
说明:这只是一个简单的模板,还有很大的扩展空间,谨慎使用,因为你不知道你女朋友的前男友会不会已经用过。