2016-01-30 19 views
1

請幫助我過濾值。我嘗試與重新,但沒有幫助。謝謝!僅從以下輸出中過濾'download_url'的值 - python

{"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":"[email protected]"},"modified_by":{"type":"user","id":"262253433","name":"RZF-ICA","login":"[email protected]"},"owned_by":{"type":"user","id":"262253433","name":"RZF-ICA","login":"[email protected]"},"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} 
+0

這應該工作:'re.search(R ' 「DOWNLOAD_URL」: 「?(+)」',輸出)。集團(1)' – tjohnson

+0

文件「/ usr/lib/python2.7/re.py「,第142行,搜索 返回_compile(pattern,flags).search(string) TypeError:期望的字符串或緩衝區@tjohnson – purple

+0

錯誤消息說第二個參數你傳遞給re.search的函數需要是一個字符串。 – tjohnson

回答

1

不要使用正則表達式。使用json解析器。它會將json解析爲一個嵌套的python字典。這比使用正則表達式強大得多。

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

如果使用正則表達式,你很可能當你得到包含"字符輸入網址錯誤就結了。這是一個有效的URL:

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

+0

這是有效的。非常感謝! – purple