<pre><code>import requests
import json #??

apiString = &quot;https://www.ebi.ac.uk/ols/api/ontologies/efo/terms/http%253A%252F%252Fpurl.obolibrary.org%252Fobo%252FMONDO_0000621/children\nhttps://www.ebi.ac.uk/ols/api/ontologies/efo/terms/http%253A%252F%252Fpurl.obolibrary.org%252Fobo%252FMONDO_0000637/children\nhttps://www.ebi.ac.uk/ols/api/ontologies/efo/terms/http%253A%252F%252Fpurl.obolibrary.org%252Fobo%252FMONDO_0000653/children\nhttps://www.ebi.ac.uk/ols/api/ontologies/efo/terms/http%253A%252F%252Fpurl.obolibrary.org%252Fobo%252FMONDO_0002402/children\nhttps://www.ebi.ac.uk/ols/api/ontologies/efo/terms/http%253A%252F%252Fpurl.obolibrary.org%252Fobo%252FMONDO_0002516/children\nhttps://www.ebi.ac.uk/ols/api/ontologies/efo/terms/http%253A%252F%252Fpurl.obolibrary.org%252Fobo%252FMONDO_0002813/children&quot;

def requestIt():
    cancer_all = requests.get('https://www.ebi.ac.uk/ols/api/ontologies/efo/terms/http%253A%252F%252Fpurl.obolibrary.org%252Fobo%252FMONDO_0000376/children')
    return cancer_all.json()

if __name__ == '__main__':
callList = split('\n')
obi = [requestIt(x) for x in callList]

# e.g. 
print (obi[0]

# an alternative approach to list comprehension 
# computationally faster but will not make much difference here
obi = list(map(requestIt, callList)    
</code></pre>
<p><strong>Note</strong> If you want to parallelise it, which might be what you meant by 'all at once', you instantiate <code>Pool()</code> from <code>multiprocessor</code> via <code>map</code> and use the same comprehension but this time the function <code>requestIt</code> will spread this over your available CPUs. Thats a little bit more complicated - namely getting the result back.</p>
<p>If the apis are in a file they will load into a list nicely:</p>
<pre><code>with open ('apis.txt', 'r') as fin:
    callList = fin.readlines()
</code></pre>
<p><strong>However</strong> you want to combine the json into a single json ... you <code>import json</code> then use <code>json.loads</code> to convert the json into a dictionary, combine the dictionaries and then use <code>json.dumps</code> to convert it back to json. The code for that is a separate question altogether and the comprehension might be better expressed as a dictionary comprehension (not sure).</p>

CC BY-SA 4.0
2022-05-20T23:57:06.777Z
2022-05-21T02:46:28.647Z
2022-05-21T02:46:28.647Z
M__
<p>I am running the following Python code with <code>python requests</code> in my Jupiter notebook to get cancer oncology id's from EBI-EMBL.</p>
<pre><code>import requests
cancer_all = requests.get('https://www.ebi.ac.uk/ols/api/ontologies/efo/terms/http%253A%252F%252Fpurl.obolibrary.org%252Fobo%252FMONDO_0000376/children')
obi = cancer_all.json()
</code></pre>
<p>The above code is for one link. However I want to get results for 30 such links together.</p>
<p>Other examples of links:</p>
<pre><code>https://www.ebi.ac.uk/ols/api/ontologies/efo/terms/http%253A%252F%252Fpurl.obolibrary.org%252Fobo%252FMONDO_0000621/children
https://www.ebi.ac.uk/ols/api/ontologies/efo/terms/http%253A%252F%252Fpurl.obolibrary.org%252Fobo%252FMONDO_0000637/children
https://www.ebi.ac.uk/ols/api/ontologies/efo/terms/http%253A%252F%252Fpurl.obolibrary.org%252Fobo%252FMONDO_0000653/children
https://www.ebi.ac.uk/ols/api/ontologies/efo/terms/http%253A%252F%252Fpurl.obolibrary.org%252Fobo%252FMONDO_0002402/children
https://www.ebi.ac.uk/ols/api/ontologies/efo/terms/http%253A%252F%252Fpurl.obolibrary.org%252Fobo%252FMONDO_0002516/children
https://www.ebi.ac.uk/ols/api/ontologies/efo/terms/http%253A%252F%252Fpurl.obolibrary.org%252Fobo%252FMONDO_0002813/children

</code></pre>
<p><strong>Questions</strong></p>
<ol>
<li>Is there any way I can run all these api in one go?</li>
<li>Could I fetch the results (in json format) of these links in a single download. Thus there would be one json file for all the links?</li>
</ol>

<api><python><jupyter>
Running a list of apis via Python's request module
# Running a list of apis via Python's request module

(Asked by: username_0 on 2022-05-20)

I am running the following Python code with python requests in my Jupiter notebook to get cancer oncology id's from EBI-EMBL.
import requests
cancer_all = requests.get('https://www.ebi.ac.uk/ols/api/ontologies/efo/terms/http%253A%252F%252Fpurl.obolibrary.org%252Fobo%252FMONDO_0000376/children')
obi = cancer_all.json()

The above code is for one link. However I want to get results for 30 such links together.
Other examples of links:
https://www.ebi.ac.uk/ols/api/ontologies/efo/terms/http%253A%252F%252Fpurl.obolibrary.org%252Fobo%252FMONDO_0000621/children
https://www.ebi.ac.uk/ols/api/ontologies/efo/terms/http%253A%252F%252Fpurl.obolibrary.org%252Fobo%252FMONDO_0000637/children
https://www.ebi.ac.uk/ols/api/ontologies/efo/terms/http%253A%252F%252Fpurl.obolibrary.org%252Fobo%252FMONDO_0000653/children
https://www.ebi.ac.uk/ols/api/ontologies/efo/terms/http%253A%252F%252Fpurl.obolibrary.org%252Fobo%252FMONDO_0002402/children
https://www.ebi.ac.uk/ols/api/ontologies/efo/terms/http%253A%252F%252Fpurl.obolibrary.org%252Fobo%252FMONDO_0002516/children
https://www.ebi.ac.uk/ols/api/ontologies/efo/terms/http%253A%252F%252Fpurl.obolibrary.org%252Fobo%252FMONDO_0002813/children


Questions

Is there any way I can run all these api in one go?
Could I fetch the results (in json format) of these links in a single download. Thus there would be one json file for all the links?

**Tags:** api, python, jupyter

---

## 1 answers

### Answer by username_1 on 2022-05-20. Score: 1 | Accepted answer

import requests
import json #??

apiString = "https://www.ebi.ac.uk/ols/api/ontologies/efo/terms/http%253A%252F%252Fpurl.obolibrary.org%252Fobo%252FMONDO_0000621/children\nhttps://www.ebi.ac.uk/ols/api/ontologies/efo/terms/http%253A%252F%252Fpurl.obolibrary.org%252Fobo%252FMONDO_0000637/children\nhttps://www.ebi.ac.uk/ols/api/ontologies/efo/terms/http%253A%252F%252Fpurl.obolibrary.org%252Fobo%252FMONDO_0000653/children\nhttps://www.ebi.ac.uk/ols/api/ontologies/efo/terms/http%253A%252F%252Fpurl.obolibrary.org%252Fobo%252FMONDO_0002402/children\nhttps://www.ebi.ac.uk/ols/api/ontologies/efo/terms/http%253A%252F%252Fpurl.obolibrary.org%252Fobo%252FMONDO_0002516/children\nhttps://www.ebi.ac.uk/ols/api/ontologies/efo/terms/http%253A%252F%252Fpurl.obolibrary.org%252Fobo%252FMONDO_0002813/children"

def requestIt():
    cancer_all = requests.get('https://www.ebi.ac.uk/ols/api/ontologies/efo/terms/http%253A%252F%252Fpurl.obolibrary.org%252Fobo%252FMONDO_0000376/children')
    return cancer_all.json()

if __name__ == '__main__':
callList = split('\n')
obi = [requestIt(x) for x in callList]

# e.g. 
print (obi[0]

# an alternative approach to list comprehension 
# computationally faster but will not make much difference here
obi = list(map(requestIt, callList)    

Note If you want to parallelise it, which might be what you meant by 'all at once', you instantiate Pool() from multiprocessor via map and use the same comprehension but this time the function requestIt will spread this over your available CPUs. Thats a little bit more complicated - namely getting the result back.
If the apis are in a file they will load into a list nicely:
with open ('apis.txt', 'r') as fin:
    callList = fin.readlines()

However you want to combine the json into a single json ... you import json then use json.loads to convert the json into a dictionary, combine the dictionaries and then use json.dumps to convert it back to json. The code for that is a separate question altogether and the comprehension might be better expressed as a dictionary comprehension (not sure).


---

