-1
import urllib2
data = []
req=urllib2.Request("https://raw.githubusercontent.com/plotly/datasets/master/miserables.json")
opener = urllib2.build_opener()
f = opener.open(req)
data = json.loads(f.read())

How to maintain the same functionality using urllib3?

Ananya Bal
  • 11
  • 2

1 Answers1

0

I find requests or aiohttp as more superior to urllibX in functionality. Could you not do:

import requests

URL = 'https://raw.githubusercontent.com/plotly/datasets/master/miserables.json'
r = requests.get(URL)

if r.ok:
    data = r.json()
else:
    #raise error
    print('Something fishy')

If you wish to work with data, Pandas is awesome for that:

import requests
import pandas as pd

URL = 'https://raw.githubusercontent.com/plotly/datasets/master/miserables.json'
r = requests.get(URL)

if r.ok:
    data = r.json()
else:
    #raise error
    print('Something fishy')

df_nodes = pd.DataFrame(data['nodes'])

df_links = pd.DataFrame(data['links'])

# Do something awesome
Prayson W. Daniel
  • 14,191
  • 4
  • 51
  • 57