Gabriele Salvatori
Gabriele Salvatori

Reputation: 460

Parse requests.post object

How can i parse the entire output takes from a request.post object and extract only the "id" content, considering this piece of code?

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'}) 

adding output.text it gives me this:

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

now I just need to extract the link in the id field, i also tried to put the content in a file and parse it as strings with file.read() but seems not work. Any ideas?

Upvotes: 2

Views: 2263

Answers (1)

alecxe
alecxe

Reputation: 473983

Load it into dictionary using json module:

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

Upvotes: 5

Related Questions