gpt4 book ai didi

python - 我想从给定的 url 中获取 json 数据,而我必须将 json 数据转换成 xml 形式

转载 作者:数据小太阳 更新时间:2023-10-29 02:12:37 26 4
gpt4 key购买 nike

我想从给定的 url 中获取 JSON 数据

http://www.deanclatworthy.com/imdb/?=The+Green+Mile

并将 JSON 数据转换为 XML。我用过urllibjson将 JSON 对象转换为 Python 字典。

这是我的代码:

import json

json_string = '{"imdbid":"tt0120689","imdburl":"http:\/\/www.imdb.com\/title\/tt0120689\/","genres":"Crime,Drama,Fantasy,Mystery","languages":"English ,French","country":"USA","votes":"281023","stv":0,"series":0,"rating":"8.4","title":"The Green Mile","year":"1999","usascreens":2875,"ukscreens":340}'

new_python_object = json.loads(json_string)
print(json_string)
print()
print (new_python_object)

结果:

{"imdbid":"tt0120689","imdburl":"http:\/\/www.imdb.com\/title\/tt0120689\/","genres":"Crime,Drama,Fantasy,Mystery","languages":"English ,French","country":"USA","votes":"281023","stv":0,"series":0,"rating":"8.4","title":"The Green Mile","year":"1999","usascreens":2875,"ukscreens":340}

{'ukscreens': 340, 'rating': '8.4', 'genres': 'Crime,Drama,Fantasy,Mystery', 'title': 'The Green Mile', 'series': 0, 'imdbid': 'tt0120689', 'year': '1999', 'votes': '281023', 'languages': 'English ,French', 'stv': 0, 'country': 'USA', 'usascreens': 2875, 'imdburl': 'http://www.imdb.com/title/tt0120689/'}

最佳答案

使用 requestsdict2xml库:

>>> import requests
>>> r = requests.get("http://www.deanclatworthy.com/imdb/?q=The+Green+Mile")
>>> import dict2xml
>>> xml = dict2xml.dict2xml(r.json)
>>> print xml
<country>USA</country>
<genres>Crime,Drama,Fantasy,Mystery</genres>
<imdbid>tt0120689</imdbid>
<imdburl>http://www.imdb.com/title/tt0120689/</imdburl>
<languages>English,French</languages>
<rating>8.5</rating>
<runtime>189min</runtime>
<series>0</series>
<stv>0</stv>
<title>The Green Mile</title>
<ukscreens>340</ukscreens>
<usascreens>2875</usascreens>
<votes>344054</votes>
<year>1999</year>

关于python - 我想从给定的 url 中获取 json 数据,而我必须将 json 数据转换成 xml 形式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12627630/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com