加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Python > 正文

python – 我想从给定的URL获取json数据而且我必须将json数据转

发布时间:2020-12-20 13:30:20 所属栏目:Python 来源:网络整理
导读:我想从给定的URL获取 JSON数据 http://www.deanclatworthy.com/imdb/?=The+Green+Mile 并将JSON数据转换为XML.我使用 urllib 和 json 将JSON对象转换为python字典. 这是我的代码: import jsonjson_string = '{"imdbid":"tt0120689","imdburl":"http://www
我想从给定的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","ukscreens":340}

{'ukscreens': 340,'rating': '8.4','genres': 'Crime,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/'}

解决方法

使用 requests和 dict2xml库:

>>> 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,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>

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读