Question

I've looked everywhere, but can't seem to find anything that answers my problem. I'm fairly new to Python, so maybe I am not understanding something correctly. The error I keep getting, is "AttributeError: Element instance has no attribute 'firstchild'"

# Imports
import urllib2
import re
from xml.dom import minidom


def main():
    pass

if __name__ == '__main__':
    main()


# Get RSS feed source
briefingRSS = minidom.parse(urllib2.urlopen('http://rss.briefing.com/Investor/RSS/UpgradesDowngrades.xml'))

# Find each Upgrade and Downgrade listed in XML file
channel = briefingRSS.getElementsByTagName("channel")[0]
items = channel.getElementsByTagName("item")

# Get info from each item
for item in items:
    getTicker = item.getElementsByTagName("title")[0].firstchild.data
    ticker = str(getTicker[1].split("<")[0])
    print ticker

Edit: Alright, thank you for pointing out the C in firstchild. But turns out, the program is spitting out one letter per line. I'm trying to capture a ticker, which can be up to 5 characters long at times. How do I get it to give me a full ticker?

Here is a snippet from the current XML for an item:

<image>
<url>http://rss.briefing.com/favicon.ico</url>
<title>Briefing.com - Upgrades Downgrades Calendar</title>
<link>
http://www.briefing.com/Investor/Public/Calendars/UpgradesDowngrades.htm
</link>
</image>
Was it helpful?

Solution

The firstChild property needs a capital letter 'C' in the middle.

The documentation isn't very clear, because it is written in terms of the DOM standard and how to map the standard to Python, so it can help just to open up the minidom.py source and see the methods and properties it defines and uses.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top