1

I have used urllib to get the following data:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<videos xmlns:xs="http://www.w3.org/2001/XMLSchema" 
        xmlns:www="http://www.www.com"">
  <video type="cl">
    <cd>
      <src lang="music">http://www.google.com/ </src>
    </cd>
  </video>
</videos>

I want to get http://www.google.com/ out, here is my code:

import xml.etree.ElementTree as etree
data='<?xml version="1.0" encoding="UTF-8" standalone="yes"?><videos xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:www="http://www.www.com""><video type="cl"><cd><src lang="music">http://www.google.com/ </src></cd></video></videos>'
tree = etree.fromstring(data)
geturl=tree.findtext('/video/cd/src').strip()
print geturl

I get error:

AttributeError: 'NoneType' object has no attribute 'strip'

Obviously, the findtext failed. I tried findtext('src'), also wont work.

Whats wrong?

1
  • What's the traceback for the error? Also, running this code does not produce the same error. Commented Aug 12, 2011 at 0:19

1 Answer 1

2

Remove the first forward-slash from the path: video/cd/src:

import xml.etree.ElementTree as etree
data='''<?xml version="1.0" encoding="UTF-8" standalone="yes"?><videos xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:www="http://www.www.com"><video type="cl"><cd><src lang="music">http://www.google.com/ </src></cd></video></videos>'''
tree = etree.fromstring(data)
geturl=tree.findtext('video/cd/src').strip()
print geturl

yields

http://www.google.com/

The forward-slash indicates an absolute path, which is not allowed on elements.

PS. There is also a syntax error in the data you posted: xmlns:www="http://www.www.com"" has two double-quotes at the end...

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

1 Comment

Thanks for your reply. I didnt use the first forward-slash in my code. It is just a typo here. I solved my problem with Beautifulsoup. The most possible reason is informal xml that ElementTree cant deal with.

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.