Hi,

Have adapted some code I found on the net to get a rss feed from a news site. I would like to email the output of this script using smtplib - after a couple of frustrating hours I have got that working as a separate element with my gmail account.
In order to email the output of the code below, do I need to save the output as a text file?
Have done some googling and from what I can gather the answer lies with redirecting the sys.__stdout__ by assigning a writable object, but having some trouble understanding how to do that. Is that the way it should be done, or have I got the wrong idea entirely?
NB: For those that haven't guessed, I'm an absolute beginner; started playing with Python a few days ago and loving it so far!

Blair

import urllib
import sys
import xml.dom.minidom

#The url of the feed
address = 'http://www.stuff.co.nz/rss/'

#Our actual xml document
document = xml.dom.minidom.parse(urllib.urlopen(address))
for item in document.getElementsByTagName('item'):
    title = item.getElementsByTagName('title')[0].firstChild.data
    link = item.getElementsByTagName('link')[0].firstChild.data
    description = item.getElementsByTagName('description')[0].firstChild.data
    
    print '''<a href="%s">%s</a> (%s)''' % (link.encode('UTF8', 'replace'),
                                            title.encode('UTF8','replace'),
                                            description.encode('UTF8','replace'))

You can send your total information gathered as a text string ...

# gathering information from the internet

import urllib
import sys
import xml.dom.minidom

# The url of the feed
address = 'http://www.stuff.co.nz/rss/'

# Our actual xml document
document = xml.dom.minidom.parse(urllib.urlopen(address))
# create an empty string to accumulate information in
info = ""
for item in document.getElementsByTagName('item'):
    title = item.getElementsByTagName('title')[0].firstChild.data
    link = item.getElementsByTagName('link')[0].firstChild.data
    description = item.getElementsByTagName('description')[0].firstChild.data
    # added newline char to make things more readable
    info_str = '''<a href="%s">%s</a> (%s)\n''' % \
        (link.encode('UTF8', 'replace'),
        title.encode('UTF8','replace'),
        description.encode('UTF8','replace'))
    # test each info line
    #print(info_str)
    # accumulate/concatinate total info as a text string
    info += info_str
    
# check result
print(info)

"""
part of my result -->
<a href="http://www.stuff.co.nz//2078576/Lost-Warriors-wish-fulfilled">Lost Warrior's wish fulfilled</a> (In death Sonny Fai should achieve one of his greatest wishes in life - a house for his mum and dad.)
<a href="http://www.stuff.co.nz//2080282/Bars-told-to-back-off">Bars told to back off</a> (Bars are using social networking sites and text messages to entice students with promises of bargain booze with prices as low as $5 for a jug of beer.)
...
"""

... or you can totally wrap into xml or html code.

Thanks Vegaseat,

Just to make sure I understand - does that mean the output does not need to be saved as a text file? If that's the case, do I send the string (info) using smtp?

Thanks,

Blair

You can send your total information gathered as a text string ...

# gathering information from the internet

import urllib
import sys
import xml.dom.minidom

# The url of the feed
address = 'http://www.stuff.co.nz/rss/'

# Our actual xml document
document = xml.dom.minidom.parse(urllib.urlopen(address))
# create an empty string to accumulate information in
info = ""
for item in document.getElementsByTagName('item'):
    title = item.getElementsByTagName('title')[0].firstChild.data
    link = item.getElementsByTagName('link')[0].firstChild.data
    description = item.getElementsByTagName('description')[0].firstChild.data
    # added newline char to make things more readable
    info_str = '''<a href="%s">%s</a> (%s)\n''' % \
        (link.encode('UTF8', 'replace'),
        title.encode('UTF8','replace'),
        description.encode('UTF8','replace'))
    # test each info line
    #print(info_str)
    # accumulate/concatinate total info as a text string
    info += info_str
    
# check result
print(info)

"""
part of my result -->
<a href="http://www.stuff.co.nz//2078576/Lost-Warriors-wish-fulfilled">Lost Warrior's wish fulfilled</a> (In death Sonny Fai should achieve one of his greatest wishes in life - a house for his mum and dad.)
<a href="http://www.stuff.co.nz//2080282/Bars-told-to-back-off">Bars told to back off</a> (Bars are using social networking sites and text messages to entice students with promises of bargain booze with prices as low as $5 for a jug of beer.)
...
"""

... or you can totally wrap into xml or html code.

No worries... after a few hours sleep it became a lot clearer.
Many thanks for your help.

Blair

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.