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


def save(path: str, data: str) -> None:
    with open(file=path, mode="w", encoding="utf-8") as file:
        file.write(data)


ids_page: list[int] = [2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19]
data: dict = {"posts": []}

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

for page_id in ids_page:
    url_page: str = f"https://new-science.ru/category/news/page/{page_id}/"

    response = requests.get(url=url_page)
    html_doc: str = response.text

    soup: BeautifulSoup = BeautifulSoup(html_doc, "html.parser")

    posts_html = soup.find(id="posts-container")
    if posts_html is None:
        raise ValueError("Мы получили None")

    posts_list_html = posts_html.find_all("li")

    for post in posts_list_html:
        detail = post.h2
        description_tag = post.find("p")

        if detail is None:
            continue

        link_tag = detail.find("a")

        if link_tag is None and description_tag is None:
            continue

        title = link_tag.get_text()
        description = description_tag.get_text()
        link = url_main + detail.a["href"]
        struct = {"title:": title, "description:": description, "link": link}
        data["posts"].append(struct)

with open("data.json", "w", encoding="utf-8") as file:
    import json

    json.dump(fp=file, obj=data, indent=2, ensure_ascii=False)