dump() 缺少 1 个必需的位置参数:python json 中的“fp”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/46396827/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
dump() missing 1 required positional argument: 'fp' in python json
提问by Aaditya Ura
I am trying to prettify the json format but i am getting this error:
我正在尝试美化 json 格式,但出现此错误:
import requests as tt
from bs4 import BeautifulSoup
import json
get_url=tt.get("https://in.pinterest.com/search/pins/?rs=ac&len=2&q=batman%20motivation&eq=batman%20moti&etslf=5839&term_meta[]=batman%7Cautocomplete%7Cundefined&term_meta[]=motivation%7Cautocomplete%7Cundefined")
soup=BeautifulSoup(get_url.text,"html.parser")
select_css=soup.select("script#jsInit1")[0]
for i in select_css:
print(json.dump(json.loads(i),indent=4,sort_keys=True))
Basically i want to extract this type of element :
基本上我想提取这种类型的元素:
'orig': {'width': 1080, 'url': '', 'height': 1349},
I know i can do this with
我知道我可以做到这一点
select_css.get('orig').get('url')
But i am not sure is this json element is nested element under any element ? That's why i am trying to prettify to get idea.
但我不确定这个 json 元素是否是任何元素下的嵌套元素?这就是为什么我试图美化以获得想法。
回答by Hou Lu
Use json.dumps()
instead. json.dump()
needs a file object and dump JSON to it.
使用json.dumps()
来代替。json.dump()
需要一个文件对象并将 JSON 转储到它。