Skip to content

Instantly share code, notes, and snippets.

@jezcope
Last active March 17, 2016 15:54
Show Gist options
  • Save jezcope/e01bc2e85adbba9d8981 to your computer and use it in GitHub Desktop.
Save jezcope/e01bc2e85adbba9d8981 to your computer and use it in GitHub Desktop.
Searching the figshare API from Python
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"200\n",
"[{'doi': '10.6084/m9.figshare.2065512',\n",
" 'id': 2065512,\n",
" 'published_date': '2016-12-31T21:22:29Z',\n",
" 'title': 'Synthesis and characterization of surface-modified mesoporous '\n",
" 'silica materials with β-cyclodextrin',\n",
" 'url': 'https://api.figshare.com/v2/articles/2065512'},\n",
" {'doi': '10.6084/m9.figshare.2350387',\n",
" 'id': 2350387,\n",
" 'published_date': '2016-12-31T19:49:38Z',\n",
" 'title': 'Conformational analysis, inter-molecular interactions, electronic '\n",
" 'properties and vibrational spectroscopic studies on '\n",
" '<i>cis</i>-4-hydroxy-<i>d</i>-proline',\n",
" 'url': 'https://api.figshare.com/v2/articles/2350387'},\n",
" {'doi': '10.6084/m9.figshare.2082664',\n",
" 'id': 2082664,\n",
" 'published_date': '2016-12-31T19:33:04Z',\n",
" 'title': 'Synthesis of focused library of novel aryloxyacids and pyrazoline '\n",
" 'derivatives: Molecular docking studies and antimicrobial '\n",
" 'investigation',\n",
" 'url': 'https://api.figshare.com/v2/articles/2082664'},\n",
" {'doi': '10.6084/m9.figshare.2067168',\n",
" 'id': 2067168,\n",
" 'published_date': '2016-12-31T18:49:13Z',\n",
" 'title': 'Turkish adaptation of the Fear of Spiders Questionnaire: '\n",
" 'Reliability and validity in non-clinical samples',\n",
" 'url': 'https://api.figshare.com/v2/articles/2067168'},\n",
" {'doi': '10.6084/m9.figshare.3079984',\n",
" 'id': 3079984,\n",
" 'published_date': '2016-12-31T12:55:04Z',\n",
" 'title': 'Change and prediction of income and fertility rates across '\n",
" 'countries',\n",
" 'url': 'https://api.figshare.com/v2/articles/3079984'},\n",
" {'doi': '10.6084/m9.figshare.2066418',\n",
" 'id': 2066418,\n",
" 'published_date': '2016-12-31T12:03:18Z',\n",
" 'title': 'A green approach to synthesize controllable silver nanostructures '\n",
" 'from <i>Limonia acidissima</i> for inactivation of pathogenic '\n",
" 'bacteria',\n",
" 'url': 'https://api.figshare.com/v2/articles/2066418'},\n",
" {'doi': '10.6084/m9.figshare.2917642',\n",
" 'id': 2917642,\n",
" 'published_date': '2016-12-31T11:58:38Z',\n",
" 'title': 'Density-dependent mortality in <i>Taiwania cryptomerioides</i> and '\n",
" '<i>Chamaecyparis formosensis</i> stands in Taiwan',\n",
" 'url': 'https://api.figshare.com/v2/articles/2917642'},\n",
" {'doi': '10.6084/m9.figshare.2243659',\n",
" 'id': 2243659,\n",
" 'published_date': '2016-12-31T10:46:48Z',\n",
" 'title': 'Grey wolf optimizer based regulator design for automatic '\n",
" 'generation control of interconnected power system',\n",
" 'url': 'https://api.figshare.com/v2/articles/2243659'},\n",
" {'doi': '10.6084/m9.figshare.2064501',\n",
" 'id': 2064501,\n",
" 'published_date': '2016-12-31T03:52:37Z',\n",
" 'title': 'Hydroxycinnamic acid functional ingredients and their biosynthetic '\n",
" 'genes in tubers of <i>Solanum tuberosum</i> Group Phureja',\n",
" 'url': 'https://api.figshare.com/v2/articles/2064501'},\n",
" {'doi': '10.6084/m9.figshare.3112657.v1',\n",
" 'id': 3112657,\n",
" 'published_date': '2016-09-01T14:31:45Z',\n",
" 'title': 'Optical chiral metamaterial based on the resonant behaviour of '\n",
" 'nanodiscs',\n",
" 'url': 'https://api.figshare.com/v2/articles/3112657'}]\n"
]
}
],
"source": [
"import requests as rq\n",
"import yaml\n",
"from pprint import pprint as pp\n",
"\n",
"TOKEN = yaml.load(open('token.yaml'))['token']\n",
"HEADERS = {'Authorization': 'token '+TOKEN}\n",
"\n",
"r = rq.post('https://api.figshare.com/v2/articles/search', params={'search_for': 'university of sheffield'}, headers=HEADERS)\n",
"print(r.status_code)\n",
"results = r.json()\n",
"pp(results)"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.5.1"
}
},
"nbformat": 4,
"nbformat_minor": 0
}
token: In real life this contains my actual API token
@adrianp
Copy link

adrianp commented Mar 17, 2016

Here is the amended code which send the request payload as JSON:

import requests as rq
from pprint import pprint as pp

HEADERS = {'content-type': 'application/json'}

r = rq.post('https://api.figshare.com/v2/articles/search', data='{"search_for": "university of sheffield", "page_size":20}', headers=HEADERS)
print(r.status_code)
results = r.json()
pp(results)

Not that for accessing public data you don't need a token.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment