I am trying to parse an XML dump of the Wiktionary but probably I am missing something since I don't get anything as output.
This is a similar but much shorter xml file:
<mediawiki xmlns="http://www.mediawiki.org/xml/export-0.8/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.mediawiki.org/xml/export-0.8/ http://www.mediawiki.org/xml/export-0.8.xsd" version="0.8" xml:lang="it">
<page>
<title>bigoto</title>
<ns>0</ns>
<id>24840</id>
<revision>
<id>1171207</id>
<parentid>743817</parentid>
<timestamp>2011-12-18T19:26:42Z</timestamp>
<contributor>
<username>GnuBotmarcoo</username>
<id>14353</id>
</contributor>
<minor />
<comment>[[Wikizionario:Bot|Bot]]: Sostituisco template {{[[Template:in|in]]}}</comment>
<text xml:space="preserve">== wikimarkups ==</text>
<sha1>gji6wqnsy6vi1ro8887t3bikh7nb3fr</sha1>
<model>wikitext</model>
<format>text/x-wiki</format>
</revision>
</page>
</mediawiki>
I am interest in parsing the content of the <title>
element if the <ns>
element equals 0.
This is my script
import xml.etree.ElementTree as ET
tree = ET.parse('test.xml')
root = tree.getroot()
for page in root.findall('page'):
ns = int(page.find('ns').text)
word = page.find('title').text
if ns == 0:
print word
I recommend using BeautifulSoup
where you can for something like this because it's just so easy to use.
from bs4 import BeautifulSoup as BS
# given your html as the variable 'html'
soup = BS(html, "xml")
pages = soup.find_all('page')
for page in pages:
if page.ns.text == '0':
print page.title.text
As far as I can tell here, no need to use int
to convert your <ns>
tag to an integer to compare against == 0
. Comparing against the string '0'
works just as well--even easier, in this case, since you wouldn't have to deal with conversion at all.