Reputation: 462
I am trying to read values from a text file (stored in a column) and pass them in a URL parameter. The author ID is unique and I want to iterate on the ID values from the text file.
The data type for content is a list and looks like this:
['7202106849\n', '7201570041\n', ....]
Here is the code I have written thus far (I know its not complete and I need some help with iteration and passing author IDs from content as a URL parameter)
with open('collab_id_2008.txt') as f:
content = f.readlines()
url = "https://api.elsevier.com/content/author/author_id/6602198520?view=metrics"
resp = requests.get(url, headers={'Accept':'application/json','X-ELS-APIKey': 'xxxxxxx', 'X-ELS-Insttoken': 'xxxxxxxx'})
print json.dumps(resp.json(), sort_keys=True, indent=4, separators=(',', ': '))
Upvotes: 1
Views: 2156
Reputation: 46759
Something like the following should get you going:
import requests
import json
with open('collab_id_2008.txt') as f_input:
for id in f_input:
url = "https://api.elsevier.com/content/author/author_id/{}?view=metrics".format(id.strip())
resp = requests.get(url, headers={'Accept':'application/json','X-ELS-APIKey': 'xxxxxxx', 'X-ELS-Insttoken': 'xxxxxxxx'})
print json.dumps(resp.json(), sort_keys=True, indent=4, separators=(',', ': '))
This reads your input file and substitutes the id
into your url.
To output to a file, it really depends on the formatting you need, here is one example:
with open('collab_id_2008.txt') as f_input, open('output.txt', 'w') as f_output:
for id in f_input:
url = "https://api.elsevier.com/content/author/author_id/{}?view=metrics".format(id.strip())
resp = requests.get(url, headers={'Accept':'application/json','X-ELS-APIKey': 'xxxxxxx', 'X-ELS-Insttoken': 'xxxxxxxx'})
print json.dumps(resp.json(), sort_keys=True, indent=4, separators=(',', ': '))
json.dump(resp.json(), f_output)
Upvotes: 4