DEV Community

IEATCODE
IEATCODE

Posted on

1 1

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

url = "https://en.wikipedia.org/api/rest_v1/page/random/summary"
response = requests.request("GET", url)
view raw 1.py hosted with ❤ by GitHub

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

dict = response.text
import json
test_string = dict
dict = json.loads(test_string)
view raw 2.py hosted with ❤ by GitHub

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

import requests
url = "https://en.wikipedia.org/api/rest_v1/page/random/summary"
response = requests.request("GET", url)
dict = response.text
import json
test_string = dict
dict = json.loads(test_string)
X = dict["content_urls"]["desktop"]["page"]
print(X)
view raw finished.py hosted with ❤ by GitHub

Hostinger image

Get n8n VPS hosting 3x cheaper than a cloud solution

Get fast, easy, secure n8n VPS hosting from $4.99/mo at Hostinger. Automate any workflow using a pre-installed n8n application and no-code customization.

Start now

Top comments (0)

Qodo Takeover

Introducing Qodo Gen 1.0: Transform Your Workflow with Agentic AI

Rather than just generating snippets, our agents understand your entire project context, can make decisions, use tools, and carry out tasks autonomously.

Read full post

Best practices for optimal infrastructure performance with Magento

Running a Magento store? Struggling with performance bottlenecks? Join us and get actionable insights and real-world strategies to keep your store fast and reliable.

Tune in to the full event

DEV is partnering to bring live events to the community. Join us or dismiss this billboard if you're not interested. ❤️