爬虫邮件系统

import urllib
import urllib.request
from bs4 import BeautifulSoup

def temper_crawler(soup): # 爬取温度
weather = soup.find_all(“p”, class_=“tem”) # 获得标签
try:
weather_l = weather[1].get_text().split(’\n’) # 获得标签的内容(get_text)并且去除回车(split)
weather_h = weather[0].get_text().split(’\n’)
weather = weather_l[1] + “~” + weather_h[1]
return weather
except:
return “”

def weather_crawler(soup):
weather = soup.find_all(“p”, class_=“wea”)
try:
weather_l = weather[1].get_text().split(’\n’)
weather_h = weather[0].get_text().split(’\n’)
return weather_l[0]+“转”+ weather_h[0]
except:
return “”

def get_description():
url = “http://www.weather.com.cn/weather1d/101200101.shtml” # 武汉天气对应的网址
user_agent = ‘Mozilla/4.0 (compatible; MSIE 5.5; Windows NT)’
req = urllib.request.Request(url, headers={‘User-Agent’: user_agent})
response = urllib.request.urlopen(req)
content = response.read().decode(‘UTF-8’) # 为爬虫设置伪装header
soup = BeautifulSoup(content, “lxml”)
weather_des = “秦老师提醒您,今日武汉市的天气状况如下:\n” + weather_crawler(soup) +"," +temper_crawler(soup)+"\n"
return weather_des
print(“您的邮件已发送,请注意查收”)

-- coding: UTF-8 --

from smtplib import SMTP_SSL
from weather import get_description
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.utils import formatdate
from email.header import Header
import sys

message = get_description()

#qq邮箱smtp服务器
host_server = ‘smtp.qq.com
#sender_qq为发件人的qq号码
sender_qq = ‘’
#pwd为qq邮箱的授权码
pwd = ‘’
#发件人的邮箱
sender_qq_mail = ‘’
#收件人邮箱
receiver = “”
#邮件的正文内容
mail_content = message
#邮件标题
mail_title = ‘天气预报’

#ssl登录
smtp = SMTP_SSL(host_server)
#set_debuglevel()是用来调试的。参数值为1表示开启调试模式,参数值为0关闭调试模式
smtp.set_debuglevel(0)
smtp.ehlo(host_server)
smtp.login(sender_qq, pwd)

msg = MIMEText(mail_content, “plain”, ‘utf-8’)
msg[“Subject”] = Header(mail_title, ‘utf-8’)
msg[“From”] = sender_qq_mail
msg[“To”] = receiver
smtp.sendmail(sender_qq_mail,receiver, msg.as_string())

smtp.quit()

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值