简体   繁体   中英

Python urllib3 opening url

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?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM