簡體   English   中英

解析request.post對象

[英]Parse requests.post object

考慮到這段代碼,我如何解析來自request.post對象的整個輸出並僅提取“ id”內容?

import json
import requests

API = 'https://www.googleapis.com/urlshortener/v1/url'
elem = json.dumps({'longUrl':'http://www.longurl..'})
output = requests.post(API,elem, headers = {'content-type':'application/json'}) 

添加output.text它給了我:

{
 "kind": "urlshortener#url",
 "id": "http://goo.gl/..",
 "longUrl": "http://www.longurl.."
}

現在,我只需要提取id字段中的鏈接,我還嘗試將內容放入文件中,並使用file.read()解析為字符串,但似乎不起作用。 有任何想法嗎?

使用json模塊將其加載到字典中:

data = json.loads(output.text)
print data['id']  # prints http://goo.gl/O5MIi

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM