HTTP requests

pip install requests

GET Request

import requests

r =requests.get('https://xkcd.com/1906/')

Status Code

r.status_code

Headers

r.headers

r.headers['Content-Type']

Response Content

r.text

HTML parser

'\r\n<!DOCTYPE html>\r\n<html>\r\n<head>\r\n\r\n<link href="http://www.smbc-comics.com/comiccontrol/defaultstyles.css?=2" rel="stylesheet" type="text/css" />\r\n<link rel="shortcut icon"
href="http://www.smbc-comics.com/favicon.ico" type="image/x-icon" />\r\n<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />\r\n<link rel="shortcut icon"
href="http://www.smbc-comics.com/favicon.ico" type="image/x-icon">\r\n<link rel="icon"
href="http://www.smbc-comics.com/favicon.ico"

Downloading and Saving an Image

Passing Argument in the Request

POST Request

JSON Response

Converting JSON to Python Dictionary

Converting JSON to Python dictionary and storing in a variable.

Reference : https://www.datacamp.com/community/tutorials/making-http-requests-in-python

Last updated

Was this helpful?