1

I'm using following statements to print the value of Title tag. Its working fine. But I'm also want to print <page id='...' ....... Is it possible? thanks

            <mediawiki>
          <siteinfo>
            <sitename>Wiki</sitename>
            <namespaces>
              <namespace key="-2" case="first-letter">Media</namespace>
            </namespaces>
          </siteinfo>
        <page id="31239628" orglength="6822" newlength="4524" stub="0" categories="0" outlinks="1" urls="10">
        <title>Title</title>
        <categories></categories>
        <links>15099779</links>
        <urls>
        </urls>
        <text>

        Books

        </text>
        </page>

        </mediawiki>

Here is my working code. Which print the title tag values.

import xml.etree.cElementTree as etree
tree = etree.parse('find_title.xml')
for value in tree.getiterator(tag='title'):
    print value.text

1 Answer 1

2

You can try the following:

import xml.etree.cElementTree as etree
from pprint import pprint
tree = etree.parse('find_title.xml')
for value in tree.getiterator(tag='title'):
    print value.text
for value in tree.getiterator(tag='page'):
    pprint(value.attrib)

It should output something like this:

$ python file.py
Title
{'categories': '0',
 'id': '31239628',
 'newlength': '4524',
 'orglength': '6822',
 'outlinks': '1',
 'stub': '0',
 'urls': '10'}
Sign up to request clarification or add additional context in comments.

4 Comments

Its working thanks. One more question, I need only id. So is it correct print(value.attrib['0']). thanks `
it's even simpler pprint(value.attrib['id'])
I'm looking If the <page id='...'> exists then print <title> value. tree.find("//page[id='31239628']/title").text any suggestion?
if tree.getiterator(tag='page'): look for title as above.

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.