How to use JSON in Python?

0

I am using the following code to collect the Urbandictionary site API

import requests
import json

page = requests.get("http://api.urbandictionary.com/v0/define?term=hello")
print(page.content)

Soon after execution, I get the following result:

b'{"tags":["hi","hi","hey","hey","greeting","greeting","yo","yo","goodbye","goodbye"],"result_type":"exact","list":[{"definition":"what you say when your talking casually with friends and your mom walks in the room","permalink":"http://hello.urbanup.com/69266","thumbs_up":3545,"author":"mad at the world","word":"hello","defid":69266,"current_vote":"","example":"What the hell(mom enters)-o mom.","thumbs_down":979},{"definition":"The only word on this site that has nothing to do with [sex] or [drugs]!","permalink":"http://hello.urbanup.com/2269237","thumbs_up":2132,"author":"pirates rule!!!","word":"Hello","defid":2269237,"current_vote":"","example":"person 1: hello\r\nPerson 2: hi\r\nPerson 1: goodbye\r\nPerson 2: Farewell, and may the forces of evil become confused in their eternal search for you\r\n*person 1 runs away*","thumbs_down":480},{"definition":"1.  A greeting\r\n2.  A for of incredulity","permalink":"http://hello.urbanup.com/390319","thumbs_up":723,"author":"DJ","word":"hello","defid":390319,"current_vote":"","example":"1.  \'HELLO!!!!!!\'\r\n2.  \'I satyed at home last night watching movies\'\r\n     \'... HELLO!!?!! You were supposed to come to my party last night!\'","thumbs_down":350},{"definition":"The opposite of [O-hell]\r\nYou use Hello when your pleased to see someone.","permalink":"http://hello.urbanup.com/322846","thumbs_up":797,"author":"Sam","word":"Hello","defid":322846,"current_vote":"","example":"\"Hello, It\'s you!!\"","thumbs_down":451},{"definition":"How to say Jell-O in Spanish. Like when that guy comes to the door in the Rocky Horror Picture show, and everybody tells him to say Jell-O in Spanish, and he says \"Hell-O\"","permalink":"http://hello.urbanup.com/562522","thumbs_up":422,"author":"That freakin guy","word":"hello","defid":562522,"current_vote":"","example":"<<<CRREEEEAAAAKKKK>>>> ...Hell-O.","thumbs_down":254},{"definition":"greeting to others form of friendly greeting","permalink":"http://hello.urbanup.com/379429","thumbs_up":365,"author":"Rahim Walji","word":"hello","defid":379429,"current_vote":"","example":"hello im silly","thumbs_down":225},{"definition":"(1) an almost obscelete form of greeting most associated with straightlaced people; \r\n(2) a polite way of answering a phone","permalink":"http://hello.urbanup.com/504054","thumbs_up":236,"author":"AuburnTIGER","word":"hello","defid":504054,"current_vote":"","example":"(1)Bob (sees Bill on street): \"Hello, Bill!\"\r\nBill: \"Well, hello, Bob.\"\r\n(2)(Phone rings loudly) \r\nJim: \"Hello?\"\r\nJames Watson from AT&T: \"Hi, this is James Watson from AT&T.  I\'m calling to talk to you about your long distance plan.\"","thumbs_down":166},{"definition":"What you say under your breath to your friend when a hot member of the opposite sex is walking by","permalink":"http://hello.urbanup.com/4890419","thumbs_up":137,"author":"fiubvevb","word":"Hello","defid":4890419,"current_vote":"","example":"-Hot person walks into the room-\n\nAdoring member of the opposite sex: \"hello\"","thumbs_down":73},{"definition":"A greeting.  A way of telling someone you noticed them.  Some synonyms of \"hello\" are: hi, hey, greetings, howdy, hola (Spanish for \"hello.\")","permalink":"http://hello.urbanup.com/856704","thumbs_up":129,"author":"I_Love_Me","word":"Hello","defid":856704,"current_vote":"","example":"Hello Sara.  How are you?\r\nHello Danny.  I am good.","thumbs_down":78},{"definition":"A spastic interrogative exclamation usually following complete ineptitude in the field of physics.","permalink":"http://hello.urbanup.com/626610","thumbs_up":54,"author":"Tea Cake","word":"hEllo?","defid":626610,"current_vote":"","example":"What\'s the field integral of the magnetic flux of the solenoid?\r\n\r\nDo you understand this?\r\n\r\nhEllo?","thumbs_down":33}],"sounds":["http://media.urbandictionary.com/sound/hello-7503.mp3","http://media.urbandictionary.com/sound/hello-9778.mp3","http://media.urbandictionary.com/sound/hello-9897.mp3","http://media.urbandictionary.com/sound/hello-10454.mp3","http://api.twilio.com/2008-08-01/Accounts/ACd09691b82112e4b26fce156d7c01d0ed/Recordings/RE35ab2d198fde01bc8889d6bedc7d3108.mp3","http://api.twilio.com/2008-08-01/Accounts/ACd09691b82112e4b26fce156d7c01d0ed/Recordings/RE7201bb8cc62883692ba7afc67037d5ff.mp3","http://wav.urbandictionary.com/hello-25244.wav","http://wav.urbandictionary.com/hello-37770.wav","https://api.twilio.com/2008-08-01/Accounts/ACd09691b82112e4b26fce156d7c01d0ed/Recordings/REa6a85510c89613bfc29fd715761f144c.mp3","http://wav.urbandictionary.com/hello-46397.wav"]}'

How can I get the value included in the definition?

    
asked by anonymous 08.01.2018 / 22:35

1 answer

1

If the content you got was in JSON you can use the .json () method of requests that will return a dict.

It would look like this:

page = requests.get("http://api.urbandictionary.com/v0/define?term=hello")
print(page.json())
{'sounds': ['http://media.urbandictionary.com/sound/hello-7503.mp3', 'http://media.urbandictionary.com/sound/hello-9778.mp3', 'http://media.urbandictionary.com/sound/hello-9897.mp3', 'http://media.urbandictionary.com/sound/hello-10454.mp3', 'http://api.twilio.com/2008-08-01/Accounts/ACd09691b82112e4b26fce156d7c01d0ed/Recordings/RE35ab2d198fde01bc8889d6bedc7d3108.mp3', 'http://api.twilio.com/2008-08-01/Accounts/ACd09691b82112e4b26fce156d7c01d0ed/Recordings/RE7201bb8cc62883692ba7afc67037d5ff.mp3', 'http://wav.urbandictionary.com/hello-25244.wav', 'http://wav.urbandictionary.com/hello-37770.wav', 'https://api.twilio.com/2008-08-01/Accounts/ACd09691b82112e4b26fce156d7c01d0ed/Recordings/REa6a85510c89613bfc29fd715761f144c.mp3', 'http://wav.urbandictionary.com/hello-46397.wav'], 'result_type': 'exact', 'list': [{'defid': 69266, 'author': 'mad at the world', 'word': 'hello', 'thumbs_down': 979, 'thumbs_up': 3548, 'permalink': 'http://hello.urbanup.com/69266', 'example': 'What the hell(mom enters)-o mom.', 'definition': 'what you say when your talking casually with friends and your mom walks in the room', 'current_vote': ''}, {'defid': 2269237, 'author': 'pirates rule!!!', 'word': 'Hello', 'thumbs_down': 480, 'thumbs_up': 2133, 'permalink': 'http://hello.urbanup.com/2269237', 'example': 'person 1: hello\r\nPerson 2: hi\r\nPerson 1: goodbye\r\nPerson 2: Farewell, and may the forces of evil become confused in their eternal search for you\r\n*person 1 runs away*', 'definition': 'The only word on this site that has nothing to do with [sex] or [drugs]!', 'current_vote': ''}, {'defid': 390319, 'author': 'DJ', 'word': 'hello', 'thumbs_down': 350, 'thumbs_up': 723, 'permalink': 'http://hello.urbanup.com/390319', 'example': "1.  'HELLO!!!!!!'\r\n2.  'I satyed at home last night watching movies'\r\n     '... HELLO!!?!! You were supposed to come to my party last night!'", 'definition': '1.  A greeting\r\n2.  A for of incredulity', 'current_vote': ''}, {'defid': 322846, 'author': 'Sam', 'word': 'Hello', 'thumbs_down': 451, 'thumbs_up': 797, 'permalink': 'http://hello.urbanup.com/322846', 'example': '"Hello, It\'s you!!"', 'definition': 'The opposite of [O-hell]\r\nYou use Hello when your pleased to see someone.', 'current_vote': ''}, {'defid': 562522, 'author': 'That freakin guy', 'word': 'hello', 'thumbs_down': 254, 'thumbs_up': 422, 'permalink': 'http://hello.urbanup.com/562522', 'example': '<<<CRREEEEAAAAKKKK>>>> ...Hell-O.', 'definition': 'How to say Jell-O in Spanish. Like when that guy comes to the door in the Rocky Horror Picture show, and everybody tells him to say Jell-O in Spanish, and he says "Hell-O"', 'current_vote': ''}, {'defid': 379429, 'author': 'Rahim Walji', 'word': 'hello', 'thumbs_down': 225, 'thumbs_up': 365, 'permalink': 'http://hello.urbanup.com/379429', 'example': 'hello im silly', 'definition': 'greeting to others form of friendly greeting', 'current_vote': ''}, {'defid': 504054, 'author': 'AuburnTIGER', 'word': 'hello', 'thumbs_down': 166, 'thumbs_up': 236, 'permalink': 'http://hello.urbanup.com/504054', 'example': '(1)Bob (sees Bill on street): "Hello, Bill!"\r\nBill: "Well, hello, Bob."\r\n(2)(Phone rings loudly) \r\nJim: "Hello?"\r\nJames Watson from AT&T: "Hi, this is James Watson from AT&T.  I\'m calling to talk to you about your long distance plan."', 'definition': '(1) an almost obscelete form of greeting most associated with straightlaced people; \r\n(2) a polite way of answering a phone', 'current_vote': ''}, {'defid': 4890419, 'author': 'fiubvevb', 'word': 'Hello', 'thumbs_down': 73, 'thumbs_up': 137, 'permalink': 'http://hello.urbanup.com/4890419', 'example': '-Hot person walks into the room-\n\nAdoring member of the opposite sex: "hello"', 'definition': 'What you say under your breath to your friend when a hot member of the opposite sex is walking by', 'current_vote': ''}, {'defid': 856704, 'author': 'I_Love_Me', 'word': 'Hello', 'thumbs_down': 78, 'thumbs_up': 129, 'permalink': 'http://hello.urbanup.com/856704', 'example': 'Hello Sara.  How are you?\r\nHello Danny.  I am good.', 'definition': 'A greeting.  A way of telling someone you noticed them.  Some synonyms of "hello" are: hi, hey, greetings, howdy, hola (Spanish for "hello.")', 'current_vote': ''}, {'defid': 626610, 'author': 'Tea Cake', 'word': 'hEllo?', 'thumbs_down': 33, 'thumbs_up': 54, 'permalink': 'http://hello.urbanup.com/626610', 'example': "What's the field integral of the magnetic flux of the solenoid?\r\n\r\nDo you understand this?\r\n\r\nhEllo?", 'definition': 'A spastic interrogative exclamation usually following complete ineptitude in the field of physics.', 'current_vote': ''}], 'tags': ['hi', 'hi', 'hey', 'hey', 'greeting', 'greeting', 'yo', 'yo', 'goodbye', 'goodbye']}

Edit: As the .json () function returns a dict you must access with the keys in your case you want the definition.

page = requests.get("http://api.urbandictionary.com/v0/define?term=hello")
d = page.json()
for i in d['list']:
   print(i['definition'])

There are several definition and can be accessed through the key list

I recommend that whenever you have any questions about the json format, use some formatting tool, such as link . of link there.

    
10.01.2018 / 17:38