python_rag_system/lesson_6/code/scraping.py
2025-10-11 19:44:31 +03:00

24 lines
959 B
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# Установка и базовое использование
# https://beautiful-soup-4.readthedocs.io/en/latest/#quick-start
from bs4 import BeautifulSoup
import requests
import json
# Получение HTML страницы URI
url = "https://python-academy.org/ru/guide/oop"
response = requests.get(url)
html = response.text
# То что ищем в html
# <li><strong class="sc-7bcc833-0 VfRFo">Модульность</strong> — код разделен на логические блоки (классы), которые легче понимать и поддерживать</li>
soup = BeautifulSoup(html, 'html.parser')
# Найти все элементы li в контейнере OL
elements = soup.select('ol > li')
mass = []
for row in elements:
mass.append(row.text.split(""))
# Сериализуем массив в json формат и сохранем в файле
fd = open("db.py", "w")
json.dump(mass, fd)
fd.close()