python获取url的json数据,通过url获取json数据并在python中使用(simplejson)

I imagine this must have a simple answer, but I am struggling: I want to take a url (which outputs json) and get the data in a usable dictionary in python. I am stuck on the last step.

>>> import urllib2

>>> import simplejson

>>> req = urllib2.Request("http://vimeo.com/api/v2/video/38356.json", None, {'user-agent':'syncstream/vimeo'})

>>> opener = urllib2.build_opener()

>>> f = opener.open(req)

>>> f.read() # this works

'[{"id":"38356","title":"Forgetfulness - Billy Collins Animated Poetry","description":"US Poet Laureate Billy Collins reads his poem ","url":"http:\\/\\/vimeo.com\\/38356","upload_date":"2006-01-24 15:21:03","thumbnail_small":"http:\\/\\/80.media.vimeo.com\\/d1\\/5\\/47\\/74\\/thumbnail-4774968.jpg","thumbnail_medium":"http:\\/\\/80.media.vimeo.com\\/d1\\/5\\/46\\/85\\/thumbnail-4685118.jpg","thumbnail_large":"http:\\/\\/images.vimeo.com\\/87\\/39\\/873998\\/873998_640x480.jpg","user_name":"smjwt","user_url":"http:\\/\\/vimeo.com\\/smjwt","user_portrait_small":"http:\\/\\/bitcast.vimeo.com\\/vimeo\\/portraits\\/defaults\\/d.30.jpg","user_portrait_medium":"http:\\/\\/bitcast.vimeo.com\\/vimeo\\/portraits\\/defaults\\/d.75.jpg","user_portrait_large":"http:\\/\\/bitcast.vimeo.com\\/vimeo\\/portraits\\/defaults\\/d.100.jpg","user_portrait_huge":"http:\\/\\/bitcast.vimeo.com\\/vimeo\\/portraits\\/defaults\\/d.300.jpg","stats_number_of_likes":"281","stats_number_of_plays":"9173","stats_number_of_comments":23,"duration":"112","width":"320","height":"240","tags":"poetry, poet, online poetry, famous poet, video poetry, modern poetry, famous poem, poetry sites, poetry websites, audio poetry, american poet, animation clips, american poetry, free poetry sites, animation art, free poetry, animated clips, poem, poet laureate"}]'

>>> simplejson.load(f)

Traceback (most recent call last):

File "", line 1, in

File "/usr/lib/python2.5/site-packages/django/utils/simplejson/__init__.py", line 298, in load

parse_constant=parse_constant, **kw)

File "/usr/lib/python2.5/site-packages/django/utils/simplejson/__init__.py", line 338, in loads

return _default_decoder.decode(s)

File "/usr/lib/python2.5/site-packages/django/utils/simplejson/decoder.py", line 326, in decode

obj, end = self.raw_decode(s, idx=_w(s, 0).end())

File "/usr/lib/python2.5/site-packages/django/utils/simplejson/decoder.py", line 344, in raw_decode

raise ValueError("No JSON object could be decoded")

ValueError: No JSON object could be decoded

Any ideas where I am going wrong?

解决方案

Try

f = opener.open(req)

simplejson.load(f)

without running f.read() first. When you run f.read(), the filehandle's contents are slurped so there is nothing left when your call simplejson.load(f)

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
回答: 在Python,可以使用json库来解析JSON数据。这个库可以将JSON字符串或文件解析为Python字典或列表,并且还可以将Python字典或列表转换为JSON字符串。如果你使用的是Python 2.6版本或更高版本,你可以直接使用内置的json库。例如,你可以使用json.loads()函数将JSON字符串解析为Python字典或列表。如果你使用的是早期版本的Python,你可以通过PyPI获取simplejson库来进行JSON解析。\[1\]下面是一个简单的示例代码,展示了如何使用json库解析JSON数据: import json json_str = '{"name": "John", "age": 30, "city": "New York"}' data = json.loads(json_str) print(data\["name"\]) # 输出: John print(data\["age"\]) # 输出: 30 print(data\["city"\]) # 输出: New York 在上面的代码,我们首先定义了一个JSON字符串json_str,然后使用json.loads()函数将其解析为Python字典data。接下来,我们可以通过data字典来访问JSON数据的各个字段。例如,data\["name"\]将返回"name"字段的值,即John。\[2\]希望这个例子能帮助你理解如何在Python解析JSON数据。 #### 引用[.reference_title] - *1* *2* [Python解析json数据](https://blog.csdn.net/xiaoyaozi2020/article/details/117267162)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值