4

I'm trying to process an XML file, but I'm getting this error:

XPathEvalError: Undefined namespace prefix

in this line:

print "category =", item.xpath("./g:google_product_category")

This is is the XML file:

<rss xmlns:g="http://base.google.com/ns/1.0" version="2.0">
<channel>
<title>example.net.br</title>
<link>http://www.example.net.br/</link>
<description>Data feed description.</description>
<item>
<title>
<![CDATA[
example
]]>
</title>
<link>
<![CDATA[
example
]]>
</link>
<description>
<![CDATA[
example]]>
</description>
<g:google_product_category>
<![CDATA[
example
]]>
</g:google_product_category>
...

This is my code:

headers = { 'User-Agent' : 'Mozilla/5.0' }
req = urllib2.Request(feed_url, None, headers)
file = urllib2.urlopen(req).read()

file = etree.fromstring(file)
for item in file.xpath('/rss/channel/item'):
    print "title =", item.xpath("./title/text()")[0]
    print "link =", item.xpath("./link/text()")[0]
    print "description =", item.xpath("./description/text()")[0]
    print "category =", item.xpath("./g:google_product_category")

How can I fix this?

1 Answer 1

5

The xpath method accepts an additional parameter: namespaces

Could you try to modify the line as follows:

print "category =", item.xpath("./g:google_product_category", namespaces={'g': 'http://base.google.com/ns/1.0'})

Source of the information available here

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

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.