Python urllib3开启网址

时间:2018-10-28 07:20:51

标签: python urllib3

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())

如何使用urllib3维护相同的功能?

1 个答案:

答案 0 :(得分:0)

我发现请求或aiohttp在功能上比urllibX更优越。你能不能做:

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')

如果您希望使用数据,Pandas对此非常棒:

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