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
requests.getwithparamsbuilds the query string.raise_for_statusturns 4xx/5xx into exceptions..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.
Step 1 of 5 in requests, step 1 of 9 in Web scraping.