郵箱設置
在使用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,
}
說明:這只是一個簡單的模板,還有很大的擴展空間,謹慎使用,因爲你不知道你女朋友的前男友會不會已經用過。