-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparser1.py
More file actions
28 lines (24 loc) · 1.09 KB
/
Copy pathparser1.py
File metadata and controls
28 lines (24 loc) · 1.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
from unittest.mock import AsyncMock, patch
import aiohttp
import asyncio
@patch('aiohttp.ClientSession.get', new_callable=AsyncMock)
async def main(mock_get):
response_mock = AsyncMock()
response_mock.status = 200
response_mock.text = AsyncMock(return_value="""
<html>
<h1 data-quote-content="true">Жизнь — это путешествие.</h1>
<div class="blockquote-origin"><a>Толстой</a></div>
</html>
""")
# Настраиваем `get`, чтобы он корректно работал с `async with`
mock_get.return_value.__aenter__.return_value = response_mock
# session = aiohttp.ClientSession()
# Открываем сессию и делаем запрос
async with aiohttp.ClientSession() as session:
async with await session.get("https://example.com") as response:
res = await response.text()
print(res)
await session.close()
# Запуск асинхронной функции
asyncio.run(main())