Make a HTTP request to get JSON content from website.

Create a file json_helper.py

1
2
3
4
5
6
7
8
9
import urllib2
import json

def get_json_by_user_id(user_id):
header = {'Content-Type': 'application/json',}
req = urllib2.Request('http://www.example.com/getmyjson?user_id=' + user_id)
website = urllib2.urlopen(req)

return json.loads(website.read())

Usage:

1
2
3
from json_helper import get_json_by_user_id

print get_json_by_user_id(123)

Reference: Extract Hyperlinks Using Python and PHP