2014年9月9日星期二

使用python抓取喜欢的网页内容然后邮件发送到博客

感觉每日英语很好,想把每日英语内容摘下来,放到我的博客。
以下是python代码,但是直接发送到博客会有乱码问题,先发送到邮件,再转发,没有问题。
__author__ = 'eagle'
# -*- coding: utf-8 -*-
import smtplib
import urllib2
from bs4 import BeautifulSoup
url = 'http://xue.youdao.com/w'
html_doc = urllib2.urlopen(url).read()
soup = BeautifulSoup(html_doc)
# The below code never changes, though obviously those variables need values.
GMAIL_USERNAME='longfengfirst@gmail.com'
GMAIL_PASSWORD='***********'
email_subject='example english'
html_content = soup.find("div",class_="example english")
print html_content
html_code = str(html_content)

body_of_email= html_code

recipient='42296*****@qq.com'

session = smtplib.SMTP('smtp.gmail.com', 25)
session.ehlo()
session.starttls()
session.login(GMAIL_USERNAME, GMAIL_PASSWORD)
headers = "\r\n".join(["from: " + GMAIL_USERNAME,
                       "subject: " + email_subject,
                       "to: " + recipient,
                       "mime-version: 1.0",
                       "content-type: text/html"])

# body_of_email can be plaintext or html!
content = headers + "\r\n\r\n" + body_of_email
session.sendmail(GMAIL_USERNAME, recipient, content)

没有评论:

发表评论