简体   繁体   English

python3:从url读取json文件

[英]python3: Read json file from url

In python3, I want to load this_file , which is a json format. 在python3中,我想加载this_file ,这是一种json格式。

Basically, I want to do something like [pseudocode]: 基本上,我想做[pseudocode]之类的事情:

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

You were close: 你很亲密:

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

Just use json and requests modules: 只需使用json并请求模块:

import requests, json

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

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. 您将需要库urlllib2,json和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. 我用了一个常用的网址来练习。

Or using the standard library: 或者使用标准库:

from urllib.request import urlopen
import json

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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