-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscrapy_mail.py
More file actions
50 lines (41 loc) · 1.6 KB
/
scrapy_mail.py
File metadata and controls
50 lines (41 loc) · 1.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
import scrapy
from scrapy.selector import Selector
from scrapy.mail import MailSender
class MySpider(scrapy.Spider):
name = 'yahoo.com'
allowed_domains = ['yahoo.com']
start_urls = ['http://www.yahoo.com']
def parse(self,response):
self.log("A response is from msn site")
# selector = Selector(response=response)
# print selector.xpath('//a/@href').extract()
self.send_mail(response.body,'Finane News')
self.log('send the email successfully')
def send_mail(self, message,subject):
print "Sending mail..........."
import smtplib
from email.MIMEMultipart import MIMEMultipart
from email.MIMEText import MIMEText
from email.MIMEBase import MIMEBase
from email import Encoders
gmailUser = 'TBD'
gmailPassword = 'TBD'
recipient = 'TBD'
msg = MIMEMultipart()
msg['From'] = gmailUser
msg['To'] = recipient
msg['Subject'] = subject
msg.attach(MIMEText('This is the latest news'))
part = MIMEBase('application', "octet-stream")
part.set_payload(message)
Encoders.encode_base64(part)
part.add_header('Content-Disposition', 'attachment; filename="%s"' % 'finance.html')
msg.attach(part)
mailServer = smtplib.SMTP('smtp.gmail.com', 587)
mailServer.ehlo()
mailServer.starttls()
mailServer.ehlo()
mailServer.login(gmailUser, gmailPassword)
mailServer.sendmail(gmailUser, recipient, msg.as_string())
mailServer.close()
print "Mail sent"