T
Python outside competition :You'll need libraries. requests for requests and bs4 for results pair:pip install bs4
pip install requests
Import them and indicate the address of the resource:import requests
from bs4 import BeautifulSoup
URL = 'https://stihi.ru'
Function get_poems_data() data collection:def get_poems_data() -> dict:
r = requests.get(f'{URL}/poems/selected.html')
soup = BeautifulSoup(r.content, 'html.parser')
poems_block = soup.find('ul', type='square')
poems_links = poems_ul.find_all('li')
poems_data = []
for row in poems_links:
data = row.find_all('a')
poems_data.append({
'title': data[0].text,
'author': data[1].text,
'link': URL + data[0]['href'],
})
return poems_data
Repeat the list of vocabularies with the name, author and reference:data = get_poems_data()
>>> data
[
{'title': 'Новый путь', 'author': 'Вадим Бакулин', 'link': 'https://stihi.ru/editor/2021/02/10/617'},
{'title': 'Фонарики', 'author': 'Светлана Севрикова', 'link': 'https://stihi.ru/editor/2021/02/10/150'},
{'title': 'Талая вода', 'author': 'Мила Исаева', 'link': 'https://stihi.ru/editor/2021/02/10/1028'},
...
{'title': 'Она пыталась', 'author': 'Иван Грозный', 'link': 'https://stihi.ru/editor/2021/02/08/3106'},
{'title': 'Как лёгок день и радостен покой', 'author': 'Фокеева Елена', 'link': 'https://stihi.ru/editor/2021/02/09/4598'},
{'title': 'Как чудища, блаженные слова', 'author': 'Хубулава Григорий Геннадьевич', 'link': 'https://stihi.ru/editor/2021/02/08/7177'}
]
Function get_poem_text(poem_link) text:def get_poem_text(poem_link: str) -> str:
r = requests.get(t_url)
soup = BeautifulSoup(r.content, 'html.parser')
text = soup.find('div', class_='text').text
return text
returns the text, e.g. the last poem data:>>> print(get_poem_text(data[-1]['link']))
Как чудища, блаженные слова,
Бросаются ко мне голодной стаей,
Строфу железной рифмой подковав,
Я их ловлю и долго приручаю.
Ныряю в детский, сумасшедший гам,
Где смех уже неотличим от стона,
И громко называю, как Адам,
Жар-птицу, саламандру и дракона.
Рычат они, напав на чужака,
Как будто им кошмар докучный снится,
И сами выбирают ездока,
Когда ему готовы подчиниться.
Строптивы, непокорны до конца,
Никто из них добром служить не хочет...
Рождается из схватки пара строчек,
Как золото из серого свинца.
And put it in the file:with open(f"{data[-1]['title']}.txt", "w") as file:
file.write(get_poem_text(data[-1]['link']))
In the directory where the file came from, there will be a new txt: Как чудища, блаженные слова.txt♪ ♪ ♪