Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
613 views
in Technique[技术] by (71.8m points)

dump() missing 1 required positional argument: 'fp' in python json

I am trying to prettify the json format but i am getting this error:

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.

question from:https://stackoverflow.com/questions/46396827/dump-missing-1-required-positional-argument-fp-in-python-json

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Use json.dumps() instead. json.dump() needs a file object and dump JSON to it.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...