0

API Request: http://iss.ndl.go.jp/api/opensearch?isbn=9784334770051

I need to get one specific value from the XML that provided through API from the above. I try to get the all value from the <item> and also the <dc:title>'s value specifically Following are my current code that I were attempt to get all value from items but didn't work.

import codecs
import sys
import urllib
import urllib2
import re, pprint
from xml.etree.ElementTree import *
import csv
from xml.dom import minidom
import xml.etree.ElementTree as ET

 errorCheck = "0"
isbn = raw_input("Enter IBSN Number Please ")
isIsbn = len(isbn)


if isIsbn == 10 or isIsbn == 13:
    errorCheck = 1;

    url = "http://iss.ndl.go.jp/api/opensearch?isbn=%s" % isbn
    req = urllib2.Request(url)

    response = urllib2.urlopen(req)
    XmlData = response.read()
    root = ET.fromstring(XmlData)

    print(root.tag,root.attrib)

    for child in root.find('item'):
        print child.tag
        print child.attrib
        print child.text

if errorCheck == "0":
    print "It is not ISBN"
har07
  • 88,338
  • 12
  • 84
  • 137
  • Element tree but require all key are unique ! Parse as string, cos more XML files haven't full quality data pattern. – dsgdfg Sep 06 '16 at 10:37

1 Answers1

1

<item> is not direct child of the root element so you can't find it by mentioning the element directly (root.find('item')). You can use .//item to find item element anywhere within the root :

for child in root.find('.//item'):

or specify the exact path from root to <item> :

for child in root.find('channel/item'):

As for finding <dc:title> element, see : Parsing XML with namespace in Python via 'ElementTree'

Community
  • 1
  • 1
har07
  • 88,338
  • 12
  • 84
  • 137
  • THANKS`! I'l try it! – Sakai Kyoutarou Sep 06 '16 at 19:59
  • namespaces = {'dc': root} # add more as needed print root.findall('dc:title', namespaces) I currently trying but I assume I'm not understanding the link you have provided. The code you give me work fine. What I need to with parsing the ElementTree? – Sakai Kyoutarou Sep 06 '16 at 21:37
  • @KeiroKamioka Add `.//` at the beginning, for the reason I have mentioned in this answer : `root.findall('.//dc:title', namespaces)` – har07 Sep 07 '16 at 01:01
  • @KeiroKamioka Don't forget to accept the answer if it solved the problem in question : [more about accepting answer](http://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work) – har07 Sep 07 '16 at 09:04
  • Did I accept answer? I'm very new to asking question here and not sure I did it correctly. I were only using asking question as substitue for my friend who does not understand English. – Sakai Kyoutarou Sep 12 '16 at 20:41