2017-09-25 63 views
4

我試圖美化JSON格式,但我得到這個錯誤:轉儲()失蹤1個人需要的位置參數:「計劃生育」在Python 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)) 

基本上我想提取此類型元素:

'orig': {'width': 1080, 'url': '', 'height': 1349}, 

我知道我可以用

select_css.get('orig').get('url') 

做到這一點,但我不知道是該JSON元素在任何嵌套元素元素?這就是爲什麼我試圖去美化想法。

+0

你想要'轉儲',它轉儲到*字符串*,'轉儲'需要一個緩衝區,即文件處理程序。 –

回答

12

改爲使用json.dumps()json.dump()需要一個文件對象並將JSON轉儲給它。

相關問題