2

Basically what I am doing is using urllib.request to make an API call to pubmed, receive an XML file in return, and am trying to parse it with no luck.

I have tried using Element Tree and other modules with no luck. I believe there may be an issue with XML object itself.

#Imorting URL Request Modules for API Calls
#Also importing ElemenTree as it seems to be best for XML parsing

import urllib.request 
import urllib.parse 
import re 
import xml.etree.ElementTree as ET 
from urllib import request 

#Now I can make the API call.  
id_request = urllib.request.urlopen('http://eutils.ncbi.nlm.nih.gov/entrez/eutils/esummary.fcgi?db=pubmed&id=17570568')

#id_request will be an object that I'm not sure I understand?  
#id_request Returns: "<http.client.HTTPResponse object at 0x0000000003693FD0>"
#Let's now read this baby in XML format!
id_pubmed = id_request.read()

#If I look at the id_pubmed object, I not have the XML file I want to parse.

You can see what the XML file id_pubmed is calling/prints here: http://eutils.ncbi.nlm.nih.gov/entrez/eutils/esummary.fcgi?db=pubmed&id=17570568

My issue is I can't get Element Tree to parse this at all. I have tried:

tree = ET.parse(id_pubmed)
root = tree.getroot()

as well as various other suggestions from https://docs.python.org/3/library/xml.etree.elementtree.html#module-xml.etree.ElementTree

1 Answer 1

2

ET.parse() method requires either the location of the xml file (on local file system) or a file like object , but your id_pubmed seems to be a string .

In that case , you should use ET.fromstring() . Example -

root = ET.fromstring(id_pubmed)
Sign up to request clarification or add additional context in comments.

2 Comments

This seems to work! Thank you, I suspected it was an issue with the method I was trying to use on my object.
Ahh, I tried to do this earlier and it did not work. It works now!

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.