4

指定されたURLからJSONデータを取得したい

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

JSONデータをXMLに変換します。私は、JSONオブジェクトをPython辞書に変換するために使用urllibしました。json

これが私のコードです:

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/'}
4

3 に答える 3

1

リクエスト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,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>
于 2012-09-27T21:13:28.267 に答える
0

使用可能なPython辞書ができたので、それをxmlに書き込む必要があります。

あなたのxmlフォーマットはどのように見えますか?Pythonは、xmlを記述/操作するためのいくつかのツールを標準ライブラリに提供します。http://docs.python.org/library/xml.dom.minidom.html

Pythonはxmlドキュメントの構造を認識していないため、書き込みには多少の労力が必要になります。少しのグーグル調査は大いに役立つことができます。

于 2012-09-27T18:24:03.427 に答える
0

ActiveStateのレシピページ(Cory Fabreによる)には、PythondictをXMLとの間で変換するための非常に多くのエントリがあります。私は最近それを少し使用しています、そしてそれはかなりうまくいくようです。リンクは次のとおりです。

http://code.activestate.com/recipes/573463-converting-xml-to-dictionary-and-back/

これは、幸運にもJSON構造がXML構造にうまくマッピングされている場合に役立ちます。

于 2012-09-27T19:25:23.083 に答える