DEV Community

IEATCODE
IEATCODE

Posted on

The Ultimate Guide to RESTful api's in python

To start We need 1 python library Go and pip install that
pip install requests

after that, we are ready to start coding, Import requests

import requests

Next we will get the text from the URL We can do this using the request method

so, in order to get text from the url we are going to use the request method

Now go ahead and print out the response and you should see something like this {"type":"standard","title":"Jefferson, West Virginia","displaytitle":"Jefferson, West Virginia","namespace":{"id":0,"text":""},"wikibase_item":"Q377854","titles":{"canonical":"Jefferson,_West_Virginia","normalized":"Jefferson, West Virginia","display":"Jefferson, West Virginia"},"pageid":138450,"thumbnail":{"source":"https://upload.wikimedia.org/wikipedia/commons/thumb/0/0d/Kanawha_County_West_Virginia_incorporated_and_unincorporated_areas_Jefferson_highlighted.svg/320px-Kanawha_County_West_Virginia_incorporated_and_unincorporated_areas_Jefferson_highlighted.svg.png","width":320,"height":284},"originalimage":{"source":"https://upload.wikimedia.org/wikipedia/commons/thumb/0/0d/Kanawha_County_West_Virginia_incorporated_and_unincorporated_areas_Jefferson_highlighted.svg/835px-Kanawha_County_West_Virginia_incorporated_and_unincorporated_areas_Jefferson_highlighted.svg.png","width":835,"height":741},"lang":"en","dir":"ltr","revision":"1001224228","tid":"e4699110-2321-11ed-9e86-21166bf96bde","timestamp":"2021-01-18T19:52:07Z","description":"Census-designated place in West Virginia, United States","description_source":"local","coordinates":{"lat":38.37416667,"lon":-81.77861111},"content_urls":{"desktop":{"page":"https://en.wikipedia.org/wiki/Jefferson%2C_West_Virginia","revisions":"https://en.wikipedia.org/wiki/Jefferson%2C_West_Virginia?action=history","edit":"https://en.wikipedia.org/wiki/Jefferson%2C_West_Virginia?action=edit","talk":"https://en.wikipedia.org/wiki/Talk:Jefferson%2C_West_Virginia"},"mobile":{"page":"https://en.m.wikipedia.org/wiki/Jefferson%2C_West_Virginia","revisions":"https://en.m.wikipedia.org/wiki/Special:History/Jefferson%2C_West_Virginia","edit":"https://en.m.wikipedia.org/wiki/Jefferson%2C_West_Virginia?action=edit","talk":"https://en.m.wikipedia.org/wiki/Talk:Jefferson%2C_West_Virginia"}},"extract":"Jefferson is a census-designated place (CDP) in Kanawha County, West Virginia, United States, along the Kanawha River. The population was 676 at the 2010 census. Jefferson was incorporated on March 22, 1997, but was disincorporated less than a decade later on February 21, 2007.","extract_html":"<p><b>Jefferson</b> is a census-designated place (CDP) in Kanawha County, West Virginia, United States, along the Kanawha River. The population was 676 at the 2010 census. Jefferson was incorporated on March 22, 1997, but was disincorporated less than a decade later on February 21, 2007.</p>"}
print(response.text)
next we are going to extract the title value from this json text

what this code does is since python thinks that the response is a string it won't allow you to get a value from this "String" So this piece of code turns it into a dictionary so we can extract values.

Then we just use dict slicing as I refer to it to get our title

Finished code

Top comments (0)