Here is some code:
from lxml import etree
url = 'http://www.google.com/ig/api?stock=aapl'
data = etree.parse(url)
root = data.getroot()
finance = root[0]
def clean(var, num):
var = str(finance[num].attrib).split("'")[-2]
print var
I would like to return the variable as an object that I can call later.
I know what the problem is – the str() – but don’t know how to fix it as working with the variable directly doesn’t allow me to to do the necessary manipulations with .attrib and .split
Assuming you want to gather information from XML, do:
You will get a dictionary in
datavariable like:And you’ll be able to access particular pieces of info: