我刚刚完成了第一个新闻网页抓取脚本的创建,尽管代码看起来看起来并不好,但我对它还是很满意的。我想知道如何在运行脚本时通过电子邮件(gmail地址)将脚本的输出发送给自己。我试图运行smtplib,但是它对我不起作用。
这是我当前的代码:
from urllib.request import urlopen as uReq
from bs4 import BeautifulSoup as soup
from datetime import date
from dateutil import parser
import smtplib
from email.mime.text import MIMEText
my_url1 = "https://www.coindesk.com/category/business-news/legal"
my_url2 = "https://cointelegraph.com/tags/bitcoin-regulation"
my_url3 = "https://techcrunch.com/tag/blockchain/"
# Opening up the website, grabbing the page
uFeedOne = uReq(my_url1, timeout=5)
page_one = uFeedOne.read()
uFeedOne.close()
# html parser
page_soup1 = soup(page_one, "html.parser")
# grabs each publication block
containers_one = page_soup1.findAll("a", {"class": "stream-article"} )
for container_one in containers_one:
## get todays date.
## I have taken an offset as the site has older articles than today.
today = date.today().strftime("%Y, %m, %d")
## The actual datetime string is in the datetime attribute of the time tag.
date_time1 = container_one.time['datetime']
## The dateutil package parses the ISO-formatted date and returns a condensed version.
date1 = parser.parse(date_time1)
dt1 = date1.strftime("%Y, %m, %d")
## Simple comparison
if dt1 == today:
link1 = container_one.attrs['href']
publication_date1 = "published on " + container_one.time.text
title1 = container_one.h3.text
description1 = "(CoinDesk)-- " + container_one.p.text
print("link: " + link1)
print("publication_date: " + publication_date1)
print("title: ", title1)
print("description: " + description1 + " \n")
uFeedTwo = uReq(my_url2, timeout=5)
page_two = uFeedTwo.read()
uFeedTwo.close()
page_soup2 = soup(page_two, "html.parser")
containers_two = page_soup2.findAll("div",{"class": "post-preview-item-inline__content"})
for container_two in containers_two:
today = date.today().strftime("%Y, %m, %d")
date_time2 = container_two.time['datetime']
date2 = parser.parse(date_time2)
dt2 = date2.strftime("%Y, %m, %d")
title_container2 = container_two.find("span",{"class": "post-preview-item-inline__title"})
description_container2 = container_two.find("p",{"class": "post-preview-item-inline__text"}).text
if dt2 == today:
link2 = container_two.div.a.attrs['href']
publication_date2 = "published on " + date2.strftime("%b %d, %Y")
title2 = title_container2.text
description2 = "(CoinTelegraph)-- " + description_container2
print("link: " + link2)
print("publication_date: " + publication_date2)
print("title: ", title1)
print("description: " + description2 + " \n")
uFeedThree = uReq(my_url3, timeout=5)
page_three = uFeedThree.read()
uFeedThree.close()
# html parser
page_soup3 = soup(page_three, "html.parser")
# grabs each publication block
containers_three = page_soup3.findAll("div",{"class": "post-block post-block--image post-block--unread"})
for container_three in containers_three:
today = date.today().strftime("%Y, %m, %d")
date_time3 = container_three.time['datetime']
date3 = parser.parse(date_time3)
dt3 = date3.strftime("%Y, %m, %d")
keyword1 = "law"
keyword2 = "legal"
description_container3 = container_three.find("div", {"class": "post-block__content"}).text.strip()
if dt3 == today and (keyword2 in description_container3) or (keyword1 in description_container3):
link3 = container_three.header.h2.a.attrs['href']
publication_date3 = "published on " + date3.strftime("%b %d, %Y")
title3 = container_three.header.h2.a.text.strip()
description3 = "(TechCrunch)-- " + description_container3
print("link: " + link3)
print("publication_date: " + publication_date3)
print("title: ", title3)
print("description: " + description3 + " \n")
我知道我想对此做个变形:
# Open a plain text file for reading. For this example, assume that
# the text file contains only ASCII characters.
with open(textfile) as fp:
# Create a text/plain message
msg = MIMEText(fp.read())
# me == the sender's email address
# you == the recipient's email address
msg['Subject'] = 'The contents of %s' % textfile
msg['From'] = me
msg['To'] = you
# Send the message via our own SMTP server.
s = smtplib.SMTP('localhost')
s.send_message(msg)
s.quit()
答案 0 :(得分:0)
这是向使用SMTP的任何人发送邮件的代码段。
以下代码已配置为gmail SMTP。如果您有其他代码,则可以 进行配置。
import smtplib
from email.MIMEMultipart import MIMEMultipart
from email.MIMEText import MIMEText
msg = MIMEMultipart()
msg['From'] = 'me@gmail.com'
msg['To'] = 'you@gmail.com'
msg['Subject'] = 'Enter subjecy of msg here'
message = 'here is the email'
msg.attach(MIMEText(message))
# GMAIL_SMTP_HOST = 'smtp.gmail.com'
# GMAIL_SMTP_PORT = '587'
mailserver = smtplib.SMTP('smtp.gmail.com',587)
# secure our email with tls encryption
mailserver.starttls()
mailserver.sendmail('me@gmail.com','you@gmail.com',msg.as_string())
mailserver.quit()