get()- GET запросpost()- POST запросput()- PUT запросdelete()- DELETE запрос
import requests
# GET запрос
response = requests.get('https://api.example.com/data')
response = requests.get('https://api.example.com/data', params={'key': 'value'})
# POST запрос
response = requests.post('https://api.example.com/data', json={'name': 'Alice'})
response = requests.post('https://api.example.com/data', data={'name': 'Alice'})
# Заголовки
headers = {'User-Agent': 'My App', 'Authorization': 'Bearer token'}
response = requests.get(url, headers=headers)response = requests.get('https://api.example.com/data')
# Статус код
print(response.status_code) # 200, 404, 500...
# Содержимое
print(response.text) # Текст
print(response.json()) # JSON как словарь
print(response.content) # Байты
# Заголовки
print(response.headers)import json
# Преобразование в JSON
data = {'name': 'Alice', 'age': 30}
json_string = json.dumps(data)
# Преобразование из JSON
json_string = '{"name": "Alice", "age": 30}'
data = json.loads(json_string)
# Работа с файлами
with open('data.json', 'w') as f:
json.dump(data, f)
with open('data.json', 'r') as f:
data = json.load(f)# Погода (OpenWeatherMap)
api_key = "your_api_key"
city = "Moscow"
url = f"http://api.openweathermap.org/data/2.5/weather?q={city}&appid={api_key}"
response = requests.get(url)
if response.status_code == 200:
weather = response.json()
temp = weather['main']['temp']
print(f"Температура: {temp}K")try:
response = requests.get(url, timeout=5)
response.raise_for_status() # Вызвать исключение для HTTP ошибок
data = response.json()
except requests.exceptions.RequestException as e:
print(f"Ошибка запроса: {e}")
except requests.exceptions.Timeout:
print("Превышено время ожидания")
except json.JSONDecodeError:
print("Ошибка парсинга JSON")Random Cat API:
response = requests.get('https://api.thecatapi.com/v1/images/search')
cat_url = response.json()[0]['url']Numbers API:
number = 42
response = requests.get(f'http://numbersapi.com/{number}')
fact = response.textJSONPlaceholder (тестовый API):
# Получить пост
response = requests.get('https://jsonplaceholder.typicode.com/posts/1')
# Создать пост
new_post = {'title': 'My Title', 'body': 'My content', 'userId': 1}
response = requests.post('https://jsonplaceholder.typicode.com/posts', json=new_post)response = requests.get('https://example.com/image.jpg')
if response.status_code == 200:
with open('image.jpg', 'wb') as f:
f.write(response.content)session = requests.Session()
session.headers.update({'User-Agent': 'My App'})
response1 = session.get('https://api.example.com/login')
response2 = session.get('https://api.example.com/data') # Cookies сохраненыdef choose_api():
print("1. Погода")
print("2. Случайный кот")
print("3. Факт о числе")
choice = input("Выберите опцию: ")
if choice == '1':
return get_weather()
elif choice == '2':
return get_random_cat()
elif choice == '3':
return get_number_fact()- Всегда проверяйте
status_code - Используйте
timeoutдля избежания зависания - Не забывайте про API ключи и лимиты
- Обрабатывайте исключения
- Уважайте ограничения API (rate limits)