# smtp simple wail transfer protocol 简单的邮件协议
# lib library
import smtplib
# 因为需要使用这个模块 所以当前py文件的名字不能写成email.py
import email
# MIME 多用于邮箱扩充协议
from email.mime.text import MIMEText
from email.mime.image import MIMEImage
from email.mime.multipart import MIMEMultipart
# 设置邮箱的域名
HOST='smtp.qq.com'
# 设置邮件标题
SUBJECT='今天是2015年11月10日,是我10岁生日'
# 注意 发件人的邮箱必须先设置开始SMTP协议
FROM='[email protected]'
# 设置收件人邮箱,(可以一次性发送多个人)
TO='[email protected],[email protected],[email protected]'
# related 表示使用内嵌资源的形式,将邮件发送给对方
# 邮件信息 内容为空
message=MIMEMultipart('related')
#-------------------发送文本--------------
# 发送邮件主题到对方的邮箱中
# 参数
# 1.发送的内容 内容必须是字符串
# 2.内容的类型 文本类型默认为plain
# 3.内容的编码方式 使用utf-8进行编码
# message_html=MIMEText('今天是星期五,郝开新','plain','utf-8')
message_html=MIMEText('我爱我的祖国
','html','utf-8')
# 将邮件内容装入到邮件信息当中去
message.attach(message_html)
#*--------------发送图片--------------------
# rb:读取二进制文件
image_data=open('1.jpg','rb')
# 设置读取获取的二进制数据
message_image=MIMEImage(image_data.read())
# 关闭刚才打开的文件
image_data.close()
message_image.add_header('content-ID','small')
#添加图片文件到邮件信息当中去
# message.attach(message_image)
#-------------------发送图片的第二种方式-------------
message_image=MIMEText(open('1.jpg','rb').read(),'base64','utf-8')
message_image['Content-disposition']='attachment;filename="happy.jpg"'
message.attach(message_image)
#---------------添加附件--------------------
# 讲一个xlsx文档作为内容发送到对方的邮箱
# 读取Excel文件时,是以rb形式进行读取的
# 是一个二进制内容 对二进制文件需要设置默认的编码格式
# 对于MIMEText()来说 默认的编码格式就是base64
# 如果对于二进制文件来说 没有设置base64进行编码 则附件
# 的就会呈现乱码
message_xlsx=MIMEText(open('table.xlsx','rb').read(),'base64','utf-8')
# 设置文件在附件当中的名字
message_xlsx['Content-Disposition']='attachment;filename="test11111.xlsx"'
message.attach(message_xlsx)
# 设置邮件发送人
message['From']=FROM
# 设置邮件收件人
message['To']=TO
# 设置邮件标题
message['Subject']=SUBJECT
# 获取简单邮件传输协议的证书
email_cilent=smtplib.SMTP_SSL()
#设置发送人邮箱的域名和端口 端口为465
email_cilent.connect(HOST,'465')
# 注意:密码千万不要写邮箱的密码 要写邮箱的授权码
result=email_cilent.login(FROM,'whrbsbrltigqbifc')
print('登录结果',result)
# 发送邮件
# message=MIMEMultipart('related') message 为MIMEMultipart的
# 一个对象 as_sring把对象转化成字符串
email_cilent.sendmail(from_addr=FROM,to_addrs=TO.split(','),msg=message.as_string())
# 关闭邮件发送客户端
email_cilent.close()
from scrapy import signals
from scrapy.http.response.html import HtmlResponse
class MusicalMiddleware(object):
def process_request(self,request,spider):
if spider.name == "taobaobao":
spider.driver.get(request.url)
spider.driver.implicitly_wait(10)
response=HtmlResponse(url=spider.driver.current_url,
request=request,
body=spider.driver.page_source,
encoding='utf-8')
return response
****************************************************************************************
# -*- coding: utf-8 -*-
import scrapy
from ..items import TaobaoItem
from selenium import webdriver
from ..sendemail import SendEmail
class TaobaobaoSpider(scrapy.Spider):
name = 'taobaobao'
allowed_domains = ['taobao.com']
start_urls = ['https://s.taobao.com/list?spm=a21bo.2017.201867-links-6.71.5af911d9ByIT2Y&q=%E5%90%89%E4%BB%96&cat=33%2C34%2C50096795%2C50039094%2C50035966&style=grid&seller_type=taobao&sort=renqi-desc']
def __init__(self):
self.driver=webdriver.PhantomJS()
def parse(self, response):
print('//////////////////////////////')
content_list=response.xpath('//div[@class="ctx-box J_MouseEneterLeave J_IconMoreNew"]')
for x in content_list:
name=x.xpath('.//div[@class="row row-2 title"]').xpath('string(.)').extract()[0].strip('\n').replace(' ','').strip('\n')
print(name)
price=x.xpath('.//div[@class="row row-1 g-clearfix"]/div[1]/strong/text()').extract_first('')
print(price)
dian_name=x.xpath('.//div[@class="shop"]/a/span[2]/text()').extract_first('')
print(dian_name)
item=TaobaoItem()
item['name']=name
item['price']=price
item['dian_name']=dian_name
yield item
print('*********************************')
@staticmethod
def close(spider, reason):
print('111111111')
email=SendEmail()
subject="淘宝乐器"
email.send_data(subject)
return
****************************************************************************************
# -*- coding: utf-8 -*-
# Define your item pipelines here
#
# Don't forget to add your pipeline to the ITEM_PIPELINES setting
# See: https://doc.scrapy.org/en/latest/topics/item-pipeline.html
import xlwt
class TaobaoPipeline(object):
def __init__(self):
self.now=1
self.workbook=xlwt.Workbook(encoding='utf-8')
self.sheet=self.workbook.add_sheet('乐器')
self.sheet.write(0,0,'名称')
self.sheet.write(0,1,'价格')
self.sheet.write(0,2,'店铺')
def process_item(self, item, spider):
self.sheet.write(self.now,0,item['name'])
self.sheet.write(self.now,1,item['price'])
self.sheet.write(self.now,2,item['dian_name'])
self.now+=1
self.workbook.save('乐器.xls')
return item
def spider_closed(self,spider):
pass
***************************************************************************************
import smtplib
from email.mime.image import MIMEImage
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
class SendEmail(object):
def __init__(self):
self.host='smtp.qq.com'
self.port='465'
self.sender='[email protected]'
self.receive='[email protected]'
self.password='whrbsbrltigqbifc'
self.message=MIMEMultipart('related')
self.message['From']=self.sender
self.message['To']=self.receive
def send_data(self,subject):
self.message['Subject']=subject
message_text=MIMEText('-喜欢么,快来购买吧!-
','html','utf-8')
self.message.attach(message_text)
message_img = MIMEText(open('1.jpg','rb').read(), 'base64', 'utf-8')
message_img["Content-Disposition"] = 'attachment;filename="yijia.jpg"'
self.message.attach(message_img)
message_xlsx=MIMEText(open('乐器.xls','rb').read(),'base64','utf-8')
message_xlsx["Content-Disposition"]='attachment;filename="yueqi.xlsx"'
self.message.attach(message_xlsx)
self.login()
def login(self):
try:
cilent=smtplib.SMTP_SSL(self.host,self.port)
result=cilent.login(self.sender,self.password)
if result[0]==235:
print('登录成功')
cilent.sendmail(self.sender,self.receive,self.message.as_string())
print('发送成功')
except Exception as e:
print('错误原因',e)
****************************************************************************************
DOWNLOADER_MIDDLEWARES = {
'taobao.middlewares.MusicalMiddleware': 543,
}
ITEM_PIPELINES = {
'taobao.pipelines.TaobaoPipeline': 300,
}