1

I am trying to read an XML file from the Yahoo finance API. So far, I've tried the following:

 from xml.dom.minidom import parse
 #Start Get Employees
        xml = urllib.request.urlopen('https://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20yahoo.finance.stocks%20where%20symbol%3D%22wfc%22&diagnostics=true&env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys')
        dom = parse(xml.read())
        numemployees = dom.getElementsByTagName('FullTimeEmployees')
        numemployees = name[0].firstChild.nodeValue
 #End Get Employees

However, this raises an exception:

AttributeError: 'bytes' object has no attribute 'read'

I think this is because if it doesn't recognize the string, it assumes I'm passing a byte pattern. However, I am passing a string so I don't know what the problem is here.

Full Stack Trace:

Traceback (most recent call last):
  File "C:\Python34\lib\tkinter\__init__.py", line 1487, in __call__
    return self.func(*args)
  File "C:\Users\kylec\Desktop\dm\Mail Server Finder\mailserverfinder.py", line 25, in getServers
    dom = parse(xml.read())
  File "C:\Python34\lib\xml\dom\minidom.py", line 1960, in parse
    return expatbuilder.parse(file)
  File "C:\Python34\lib\xml\dom\expatbuilder.py", line 913, in parse
    result = builder.parseFile(file)
  File "C:\Python34\lib\xml\dom\expatbuilder.py", line 204, in parseFile
    buffer = file.read(16*1024)
AttributeError: 'bytes' object has no attribute 'read'
2
  • The xml.read() call isn't failing. What's the full backtrace? Where is parse coming from? Commented Jun 6, 2014 at 15:50
  • Parse is from this: from xml.dom.minidom import parse Commented Jun 6, 2014 at 15:52

2 Answers 2

1

xml.dom.minidom.parse is excepting a file-like object, not a bytes or str, as stated in its documentation:

xml.dom.minidom.parse(filename_or_file[, parser[, bufsize]])

Return a Document from the given input. filename_or_file may be either a file name, or a file-like object.

So you just need to do this:

dom = parse(xml)

Because the http.client.HTTPResponse object returned by urlopen is file-like.

Sign up to request clarification or add additional context in comments.

Comments

0

Kyle, sorry but your example isn't clear enough. I think this is what you expected to do.

from xml.dom.minidom import parseString

employees = urllib.urlopen('https://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20yahoo.finance.stocks%20where%20symbol%3D%22wfc%22&diagnostics=true&env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys').read()
dom = parseString(employees)
numemployees = dom.getElementsByTagName('FullTimeEmployees')
numemployees = numeemployees[0].firstChild.nodeValue

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.