我正在处理请求,而且我被困了,因为我正在改变我的来源。事情是,当我向网站请求对象时,它返回以下json对象:
[
{
"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"
}
]
我想更改"file_url"
"http:\/\/gelbooru.com\/images\/ca\/48\/ca4860af9e3be43b1d23b823af607be4.jpg"
值
到
"http://gelbooru.com/images/ca/48/ca4860af9e3be43b1d23b823af607be4.jpg"
所以我可以通过在我的程序构建器上调用它来完全使用该网站。
我怎么做?
在此先感谢。
答案 0 :(得分:1)
requests
可以自动将JSON数据转换为python词典/列表
r = request.get(...)
data = r.json()
然后你有权访问
print( data[0]['file_url'] )
您将看到没有\/
,因为它已转换为正确的文字。
标准json
模块的示例,内部由requests
使用。
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'] )