0

以下のコードは、ESPN/college-football から上位の見出しを抽出しています。記事自体にアクセスしてp内容を抽出すると、コンソールに問題なく出力されますが、内容をメールで送信したいと思います。何らかの理由でコンテンツが送信されません。コードは次のとおりです。

from urllib import urlopen
from BeautifulSoup import BeautifulSoup
import datetime
import smtplib

# Copy all of the content from the provided web page
webpage = urlopen('http://espn.go.com/college-football').read()
soup = BeautifulSoup(webpage)
now = datetime.datetime.now()



# to get the contents of <ul> tags w/ attribute class="headlines":
for i in soup.findAll('ul', {'class': 'headlines'}):
    for tag in i.findAll('li'):
        for a in tag.findAll({'a' : True, 'title' : False}):            
            print a.text
            print a['href']           
            print "\n"

            articlePage = urlopen(a['href']).read() # Grab all of the content from original article

            # Pass the article to the Beautiful Soup Module
            soup1 = BeautifulSoup(articlePage)

            # Tell Beautiful Soup to locate all of the p tags and store them in a list
            paragList = soup1.findAll('p')

            # Print all of the paragraphs to screen
            for z in paragList:
                print z.text

            print "\n" 

            # -*- coding: utf-8 -*-
            from email.header    import Header
            from email.mime.text import MIMEText

            msg = MIMEText(a.text + "\n" + str(a.get('href') + "\n" + z.text), 'plain', 'utf-8')
            msg['Subject'] = Header('ESPN Scrape from: '+ now.strftime("%Y-%m-%d %H:%M"), 'utf-8')
            msg['From'] = 'FROM'
            msg['To'] = 'TO'
            print(msg.as_string())   


            # Credentials (if needed)
            username = 'username'
            password = 'password'

            from smtplib import SMTP_SSL

            # send it via gmail
            s = SMTP_SSL('smtp.gmail.com')
            s.set_debuglevel(1)
            try:
                s.login(username, password)
                s.sendmail(msg['From'], msg['To'], msg.as_string())
            finally:
                s.quit()
4

1 に答える 1

0

paraglist の各メンバーをコンソールに出力するときは for ループを使用しますが、msg を作成するときは、paraglist をループせずに z.text を追加するだけです。

ただし、これはコピー貼り付けエラーである可能性があります。

于 2013-01-25T22:23:46.370 に答える