仅过滤' download_url'的值从以下输出 - python

时间:2016-01-30 17:21:12

标签: python regex

请帮助我过滤价值。我尝试了但是没有帮助。谢谢!

{"total_count":1,"entries":[{"type":"file","id":"51371323013","file_version":{"type":"file_version","id":"52926277625","sha1":"22596363b3de40b06f981fb85d82312e8c0ed511"},"sequence_id":"1","etag":"1","sha1":"22596363b3de40b06f981fb85d82312e8c0ed511","name":"hello.txt","description":"","size":12,"path_collection":{"total_count":1,"entries":[{"type":"folder","id":"0","sequence_id":null,"etag":null,"name":"All Files"}]},"created_at":"2016-01-28T23:02:21-08:00","modified_at":"2016-01-28T23:02:21-08:00","trashed_at":null,"purged_at":null,"content_created_at":"2016-01-28T23:01:42-08:00","content_modified_at":"2016-01-28T23:01:42-08:00","created_by":{"type":"user","id":"262253433","name":"RZF-ICA","login":"blah@blah.com"},"modified_by":{"type":"user","id":"262253433","name":"RZF-ICA","login":"blah@blah.com"},"owned_by":{"type":"user","id":"262253433","name":"RZF-ICA","login":"blah@blah.com"},"shared_link":{"url":"https:\/\/app.box.com\/s\/84wqj8mvwm77ern12121mbe4b68lgf","download_url":"https:\/\/app.box.com\/shared\/static\/84wqj8mvwm77ernjtkvm131b68lgf.txt","vanity_url":null,"effective_access":"company","is_password_enabled":false,"unshared_at":null,"download_count":0,"preview_count":0,"access":"company","permissions":{"can_download":true,"can_preview":true}},"parent":{"type":"folder","id":"0","sequence_id":null,"etag":null,"name":"All Files"},"item_status":"active"}],"limit":1,"offset":0}

1 个答案:

答案 0 :(得分:1)

不要使用正则表达式。使用json解析器。它会将json解析为嵌套的python字典。这比使用正则表达式更强大。

import json
data = json.loads(your_input_json_string)
print(data['entries'][0]['shared_link']['download_url'])

如果您使用正则表达式,当您获得包含"字符的输入网址时,您可能会遇到错误。这是一个有效的网址:

https://www.google.com/?q="when+not+to+use+regular+expressions"