0

What's wrong with the following code? I am expecting boogie as output.

import urllib.request
from html.parser import HTMLParser
import xml.etree.ElementTree as ET

html = '''<rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
 xmlns="http://purl.org/rss/1.0/"
 xmlns:enc="http://purl.oclc.org/net/rss_2.0/enc#"
><foo><title>boogie</title></foo></rdf:RDF>'''

root = ET.fromstring(html)
ns = { 'default': 'http://purl.org/rss/1.0/', 'rdf': 'http://www.w3.org/1999/02/22-rdf-syntax-ns#'}

titles = root.findall("default:.//title", ns)
[print(title.text) for title in titles]

1 Answer 1

1
import urllib.request
from html.parser import HTMLParser
import xml.etree.ElementTree as ET

html = '''<rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
 xmlns="http://purl.org/rss/1.0/"
 xmlns:enc="http://purl.oclc.org/net/rss_2.0/enc#"
 ><foo><title>boogie</title></foo></rdf:RDF>'''

root = ET.fromstring(html)
ns = '{http://purl.org/rss/1.0/}'

titles = root.findall(".//%stitle" % ns)
print titles[0].text

This is working version

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

1 Comment

you can change this line: titles = root.findall("default:.//title", ns) to titles = root.findall(".//default:title", ns) then it will work. You put default in wrong place

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.