Created
May 16, 2016 11:07
-
-
Save phillipkent/47080b52b15616b9852222a046ab0e9c to your computer and use it in GitHub Desktop.
Using the 'requests' module in Python, initiate a session with citeulike.org and run queries on citeulike accounts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# citeulike_init_session.py | |
# | |
# by Phillip Kent | |
# | |
# Using the 'requests' module in Python [http://docs.python-requests.org], initiate a session with citeulike.org | |
# to generate a login cookie. A simple function cquery is defined to make queries on a specified citeulike account and | |
# return results as a Python dictionary (equivalent to JSON format) | |
# | |
# You can use this interactively on the Python commandline by running: | |
# $ python -i citeulike_init_session.py | |
# | |
# https://gist.github.com/phillipkent | |
# http://www.phillipkent.net | |
import requests | |
import json | |
# ***Replace 'phillipkent' with the citeulike account name that you want make queries on*** | |
def cquery(querytags): | |
data = requests.get('http://www.citeulike.org/json/user/phillipkent' + querytags,cookies={'login':loginCookie}) | |
return data.json() | |
# Do login to citeulike.org | |
# ***Replace USERNAME and PASSWORD with your own account details*** | |
session=requests.post('http://www.citeulike.org/login.do',data='username=USERNAME&password=PASSWORD&perm=1') | |
# Extract the login cookie | |
loginCookie=session.request.headers['Cookie'].split(';')[0].split('=')[1] | |
# Use cquery to get JSON output from citeulike, for example | |
# cquery('/tag/computer-networks') | |
# cquery('/tag/computer-networks/author/hartpence') | |
# Use json.dumps() to pretty-print query results for readability, for example | |
# print json.dumps(cquery('/tag/computer-networks/author/hartpence'), indent=4, separators=(',', ': ')) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment