pip install requests # Для Windows
pip3 install requests # Для Linux, MacOS
python -m pip install requests # Установит библиотеку для последней версии Python
python3.7 -m pip install requests # Requests для Python 3.7
>>> import requests
>>> response = requests.get('https://github.com')
>>> response
<Response [200]>
>>> dir(response)
[…, 'apparent_encoding', 'close', 'connection', 'content', 'cookies', 'elapsed', 'encoding', 'headers', 'history', 'is_permanent_redirect', 'is_redirect', 'iter_content', 'iter_lines', 'json', 'links', 'next', 'ok', 'raise_for_status', 'raw', 'reason', 'request', 'status_code', 'text', 'url']
>>> response.headers
{'Server': 'GitHub.com', 'Date': 'Fri, 05 Mar 2021 09:47:19 GMT', 'Content-Type': 'text/html; charset=utf-8', …}
>>> response.status_code
200
>>> response.ok
True
>>> response.request
<PreparedRequest [GET]>
>>> response.url
'https://github.com/'
>>> response.text
'\n\n\n\n\n<!DOCTYPE html>\n<html lang="en" class="html-fluid">\n <head>\n <meta charset="utf-8">\n <link rel="dns-prefetch" href="https://github.githubassets.com">\n <link rel="dns-prefetch" href="https://user-images.githubusercontent.com/">…
>>> response.content
b'\n\n\n\n\n<!DOCTYPE html>\n<html lang="en" class="html-fluid">\n <head>\n <meta charset="utf-8">\n <link rel="dns-prefetch" href="https://github.githubassets.com">\n <link rel="dns-prefetch" href="https://avatars.githubusercontent.com">…
>>> response.json()
Error…
>>> response = requests.options('https://httpbin.org/')
>>> response.headers['Access-Control-Allow-Methods']
GET, POST, PUT, DELETE, PATCH, OPTIONS
>>> get_params = {'page': 11, 'product': 'car'}
>>> response = requests.get('https://httpbin.org/', params=get_params)
>>> response.url
'https://httpbin.org/?page=11&product=car'
>>> post_params = {'user': 'admin', 'password': 'admin_pass1'}
>>> response = requests.post('https://httpbin.org/post', data=post_params)
>>> response.json()['form']
{'password': 'admin_pass1', 'user': 'admin'}
>>> response = requests.head('https://httpbin.org/')
>>> response.headers
{'Date': 'Sat, 06 Mar 2021 11:40:49 GMT', 'Content-Type': 'text/html; charset=utf-8', 'Content-Length': '9593', 'Connection': 'keep-alive', 'Server': 'gunicorn/19.9.0', 'Access-Control-Allow-Origin': '*', 'Access-Control-Allow-Credentials': 'true'}
>>> put_params = {'user': 'admin', 'password': 'admin_pass1'}
>>> response = requests.put('https://httpbin.org/put', data=put_params)
>>> response.status_code
200
>>> response = requests.put('https://httpbin.org/', data=put_params)
>>> response.status_code
405
>>> patch_params = {'user': 'new_admin', 'password': 'new_pass'}
>>> response = requests.patch('https://httpbin.org/patch', data=patch_params)
>>> response.status_code
200
>>> del_params = {"name": "Николай", "job": "Повар"}
>>> response = requests.delete('https://httpbin.org/delete', data=del_params)
>>> response.json()['form']
{'job': 'Повар', 'name': 'Николай'}
import requests
response = requests.get('https://www.hdwallpapers.in/download/colorful_glowing_shape_lines_4k_8k_hd_abstract-HD.jpg')
with open('img.jpg', 'wb') as picture:
picture.write(response.content)
import requests
response = requests.get('https://www.hdwallpapers.in/download/colorful_glowing_shape_lines_4k_8k_hd_abstract-HD.jpg',
stream=True)
with open('img.jpg', 'wb') as picture:
for chunk in response.iter_content(chunk_size=10000):
if chunk:
picture.write(chunk)
Логин: smartiqa-user
Токен: 3b7cbc8bbe0c7866ccd003c1ba21add68f6ddb31
>>> import requests
>>> response = requests.get('https://api.github.com/user', auth=('smartiqa-user', '3b7cbc8bbe0c7866ccd003c1ba21add68f6ddb31'))
>>> response.json()
{'login': 'smartiqa-user', 'id': 713123083, …'type': 'User', 'site_admin': False, 'name': 'Smartiqa Programming and QA', 'company': None, 'blog': '', 'location': 'Russia'}
>>> response = requests.get('https://api.github.com/user/repos', auth=('smartiqa-user', '3b7cbc8bbe0c7866ccd003c1ba21add68f6ddb31'))
>>> response.json()
[{'id': 281217463, 'node_id': 'MDEwOlJlcG9zaXRvcnkyODEyMTc0NjM=', 'name': 'blog', 'full_name': 'smartiqaorg/blog', 'private': True, 'owner': {'login': 'smartiqaorg', 'id': 44243438, 'node_id': 'MDEyOk9yZ2FuaXphdGlvbjQ0MjQzNDM4', 'avatar_url': 'https://avatars.githubusercontent.com/u/44243438?v=4',
...
'archived': False, 'disabled': False, 'open_issues_count': 0, 'license': None, 'forks': 0, 'open_issues': 0, 'watchers': 0, 'default_branch': 'main', 'permissions': {'admin': True, 'push': True, 'pull': True}}]
>>> import requests
>>> response = requests.get('https://www.python.org/')
>>> response.request.headers
{'User-Agent': 'python-requests/2.25.1', 'Accept-Encoding': 'gzip, deflate', 'Accept': '*/*', 'Connection': 'keep-alive'}
>>> import requests
>>> user_agent = {'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) '
'Chrome/89.0.4389.72 Safari/537.36'}
>>> response = requests.get('https://www.python.org/', headers=user_agent)
>>> response.request.headers
{'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.72 Safari/537.36', 'Accept-Encoding': 'gzip, deflate', 'Accept': '*/*', 'Connection': 'keep-alive'}
>>> import requests
>>> user_agent = {'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) '
'Chrome/89.0.4389.72 Safari/537.36'}
>>> proxy = {'http': 'http://185.253.98.21:3128',
'https': 'http://185.253.98.21:3128'}
>>> response = requests.get('https://ramziv.com/ip')
>>> response.text
29.174.182.126
>>> session = requests.Session()
>>> session.proxies.update(proxy)
>>> session.headers.update(user_agent)
>>> response = session.get('https://ramziv.com/ip')
>>> response.text
185.253.98.21
import requests
user_agent = {'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) '
'Chrome/89.0.4389.72 Safari/537.36'}
session = requests.Session()
session.headers.update(user_agent)
def pics_downloader (num_of_pics):
for num in range(num_of_pics):
response = session.get('https://picsum.photos/200', allow_redirects=True, stream=True)
with open(f'{num}.jpg', 'wb') as picture:
for chunk in response.iter_content(chunk_size=1024):
if chunk:
picture.write(chunk)
print(f'Скачано {num + 1} фото из {num_of_pics}')
pics_downloader(20)