💻
Code Snippet
  • Overview
  • General
    • Anaconda
  • GUI
    • PyQT
      • Qt Design
  • Pandas
    • Read Data
    • Replace
  • Articles
    • Python. PyQt
    • Offline Payment Wallet With Django
    • Documentation Encrypt File
    • Play With Pillow
  • Fontend
    • Snippet
    • Hugo
    • JavaScript
      • Form Validation
  • Finance
    • Library
      • yfinance
  • Notebook
    • Untitled
    • Snippet
  • Python
    • Download file
    • Date and Time
    • Snippet
    • Compile .exe
    • Overview
    • Google
      • Samples for Google Workspace
      • Drive
      • GoogleSheet
    • Virtual environment
    • Database
      • Pickle()
    • Datatypes
      • Excel
      • Dictionary
        • xmltodict()
    • File Handling
      • shutil()
      • Get the File Name
      • Get the Full Path
      • Check the File Size
      • Get File Creation Date
      • Find All File
        • Untitled
    • Dictionary
      • Convert Two Lists
  • Data Science
    • HTTP requests
  • Google Workspace
    • Overview
    • Apps Script
      • ์Note
      • Overview
      • Snippet
        • HTML Service
        • Fetch API
      • Quickstart
      • Google Sheets
        • Overview
          • Snippet
        • Fundamentals
          • Macros & Custom Functions
          • Spreadsheets, Sheets, and Ranges
          • Working with Data
          • Data Formatting
          • Chart and Present Data
        • Built-in Google Services
        • Fetch and format API data
        • Connected Sheets
  • Git
  • Mini Lab
    • Line
    • Python
  • Function
    • Python
      • Date&Time
  • Database
    • SQLite
      • Example
Powered by GitBook
On this page
  • GET Request
  • Status Code
  • Headers
  • Response Content
  • HTML parser
  • 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.

Was this helpful?

  1. Data Science

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

import requests

receive = requests.get('https://imgs.xkcd.com/comics/making_progress.png')
with open(r'C:\Users\Dell\Desktop\comics\image5.png','wb') as f:
    f.write(recieve.content)

Passing Argument in the Request

import requests
ploads = {'things':2,'total':25}
r = requests.get('https://httpbin.org/get',params=ploads)
print(r.text)
print(r.url)
print(r.url)

POST Request

import requests
pload = {'username':'Olivia','password':'123'}
r = requests.post('https://httpbin.org/post',data = pload)
print(r.text)

JSON Response

Converting JSON to Python Dictionary

import requests
pload = {'username':'olivia','password':'123'}
r = requests.post('https://httpbin.org/post',data = pload)

print(r.json())

Converting JSON to Python dictionary and storing in a variable.

import requests
pload = {'username':'olivia','password':'123'}
r = requests.post('https://httpbin.org/post',data = pload)
r_dictionary= r.json()
print(r_dictionary['form'])

PreviousData ScienceNextGoogle Workspace

Last updated 3 years ago

Was this helpful?

Reference :

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