2016-12-06 6 views
0

私はリクエストに取り組んでいます。私はソースを変更しているので、私は固執しました。事は、私がウェブサイトにオブジェクトを要求するとき、それは次のようなJSONオブジェクトを返し、次のとおりです。このjsonオブジェクトをPythonに解析するには?

[ 
{ 
"directory":"ca\/48", 
"hash":"ca4860af9e3be43b1d23b823af607be4", 
"height":1839, 
"id":3461818, 
"image":"ca4860af9e3be43b1d23b823af607be4.jpg", 
"change":1480968006, 
"owner":"danbooru", 
"parent_id":null, 
"rating":"s", 
"sample":true, 
"sample_height":1398, 
"sample_width":850, 
"score":0, 
"tags":"1girl breasts cape cleavage defense_of_the_ancients dota_2 green_eyes highres lyralei medium_breasts parted_lips pauldrons red_hair smile solo splashbrush thick_thighs thighs toes", 
"width":1118, 
"file_url":"http:\/\/gelbooru.com\/images\/ca\/48\/ca4860af9e3be43b1d23b823af607be4.jpg" 
} 
] 

を私は

​​ に"http:\/\/gelbooru.com\/images\/ca\/48\/ca4860af9e3be43b1d23b823af607be4.jpg"

から"file_url"値を変更したいので、私は」私のプログラムのビルダーで呼び出すことで、ウェブサイトを完全に使用できるようになります。 どうすればいいですか? ありがとうございました。

+0

おそらく何百もの類似の質問があります。喜んで重複する可能性のあるものを投稿する前に、いくつかの調査を行います。 –

+0

"json object"はPythonではあまり意味がありません。 1つの項目を持つリストがあります。これはdictです。 dictを変更してリスト要素にアクセスする方法がわかっている場合は、残りの部分を見つけることができます。 – bereal

+0

'requests'はJSONをPythonのdictionary/list(' response.json')に変換し、 '\ /'を変換してテキストを修正することができます。 – furas

答えて

1

requestsは自動的にPythonの辞書/リスト

r = request.get(...) 

data = r.json() 

、その後、あなたが持っているアクセス

print(data[0]['file_url']) 

にJSONデータを変換することができますし、それがテキストを修正するために変換されたので、何の\/が存在しないことがわかります。内部requestsによって使用される標準的なjsonモジュールと


例。

text = '''[ 
{ 
"directory":"ca\/48", 
"hash":"ca4860af9e3be43b1d23b823af607be4", 
"height":1839, 
"id":3461818, 
"image":"ca4860af9e3be43b1d23b823af607be4.jpg", 
"change":1480968006, 
"owner":"danbooru", 
"parent_id":null, 
"rating":"s", 
"sample":true, 
"sample_height":1398, 
"sample_width":850, 
"score":0, 
"tags":"1girl breasts cape cleavage defense_of_the_ancients dota_2 green_eyes highres lyralei medium_breasts parted_lips pauldrons red_hair smile solo splashbrush thick_thighs thighs toes", 
"width":1118, 
"file_url":"http:\/\/gelbooru.com\/images\/ca\/48\/ca4860af9e3be43b1d23b823af607be4.jpg" 
} 
]''' 

import json 

data = json.loads(text) 

print(data[0]['file_url']) 
関連する問題