typestar

requests GET in Python

The friendly way to fetch data over HTTP.

import requests

response = requests.get(
    "https://api.example.com/search",
    params={"q": "typing", "limit": 10},
    timeout=10,
)
response.raise_for_status()

status = response.status_code
content_type = response.headers["Content-Type"]
data = response.json()
results = data["results"]

How it works

  1. requests.get with params builds the query string.
  2. raise_for_status turns 4xx/5xx into exceptions.
  3. .json() parses the response body.

The run, in numbers

Lines
13
Characters to type
282
Tokens
70
Three-star pace
100 tpm

At the three-star pace of 100 tokens a minute, this run takes about 42 seconds.

Type this snippet

Step 1 of 5 in requests, step 1 of 9 in Web scraping.

Next →