JustCaptain kod-buduschego
main
Перейти к файлу
Код
kod-buduschego Python 2025-20264.2. Веб-скрапингpracticestask_4.py
anc update c70a58f 8 октября 2026 г.
task_4.py
import requests
from bs4 import BeautifulSoup

url: str = "https://new-science.ru"

response = requests.get(url=url)
text = response.text

soup = BeautifulSoup(text, "html.parser")
posts = soup.find_all('h2')
titles: str = ""
for post in posts:
    title = post.a
    if title:
        titles = titles + title.text + "\n"

with open("titles.txt", mode="w", encoding="utf-8") as file:
    file.write(titles)