Ivan Castro
Ivan Castro

Reputation: 615

python3: Read json file from url

In python3, I want to load this_file, which is a json format.

Basically, I want to do something like [pseudocode]:

>>> read_from_url = urllib.some_method_open(this_file)
>>> my_dict = json.load(read_from_url)
>>> print(my_dict['some_key'])
some value

Upvotes: 21

Views: 33815

Answers (4)

automatist
automatist

Reputation: 61

Or using the standard library:

from urllib.request import urlopen
import json

data = json.loads(urlopen(url).read().decode("utf-8"))

Upvotes: 6

coder
coder

Reputation: 12992

You were close:

import requests
import json
response = json.loads(requests.get("your_url").text)

Upvotes: 38

Juan Albarracín
Juan Albarracín

Reputation: 143

Just use json and requests modules:

import requests, json

content = requests.get("http://example.com")
json = json.loads(content.content)

Upvotes: 9

BLang
BLang

Reputation: 990

So you want to be able to reference specific values with inputting keys? If i think i know what you want to do, this should help you get started. You will need the libraries urlllib2, json, and bs4. just pip install them its easy.

import urllib2
import json
from bs4 import BeautifulSoup

url = urllib2.urlopen("https://www.govtrack.us/data/congress/113/votes/2013/s11/data.json")
content = url.read()
soup = BeautifulSoup(content, "html.parser")
newDictionary=json.loads(str(soup))

I used a commonly used url to practice with.

Upvotes: 4

Related Questions