{"id":345573,"date":"2023-02-17T09:01:51","date_gmt":"2023-02-17T09:01:51","guid":{"rendered":"http:\/\/savepearlharbor.com\/?p=345573"},"modified":"-0001-11-30T00:00:00","modified_gmt":"2023-02-17T09:01:51","slug":"","status":"publish","type":"post","link":"https:\/\/savepearlharbor.com\/?p=345573","title":{"rendered":"<span>\u041f\u0430\u0440\u0441\u0435\u0440 \u043b\u0443\u0447\u0448\u0438\u0445 \u0437\u0430 \u0441\u0443\u0442\u043a\u0438 \u0441\u0442\u0430\u0442\u0435\u0439 habr(\u0430) \u043d\u0430 Python. \u041e\u0447\u0435\u043d\u044c \u043f\u043e\u0434\u0440\u043e\u0431\u043d\u043e \u0438 \u043f\u0440\u043e\u0441\u0442\u043e<\/span>"},"content":{"rendered":"<div><\/div>\n<div id=\"post-content-body\">\n<div>\n<div class=\"article-formatted-body article-formatted-body article-formatted-body_version-2\">\n<div xmlns=\"http:\/\/www.w3.org\/1999\/xhtml\">\n<p>\u0412\u043e-\u043f\u0435\u0440\u0432\u044b\u0445, \u044f \u043d\u0430\u0447\u0438\u043d\u0430\u044e\u0449\u0438\u0439 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0441\u0442.<\/p>\n<p>\u0412\u043e-\u0432\u0442\u043e\u0440\u044b\u0445 \u0434\u0430\u0432\u0430\u0439\u0442\u0435 \u043f\u0440\u0438\u0441\u0442\u0443\u043f\u0438\u043c.<\/p>\n<p>\u041d\u0443\u0436\u043d\u044b\u0435 \u043d\u0430\u043c \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u0438 (\u0432\u0441\u0442\u0430\u0432\u043b\u044f\u0435\u043c \u0432 \u043a\u043e\u043c\u0430\u043d\u0434\u043d\u0443\u044e \u0441\u0442\u0440\u043e\u043a\u0443, \u043b\u0438\u0431\u043e \u0432 \u0442\u0435\u0440\u043c\u0438\u043d\u0430\u043b\u0435 \u0438\u043b\u0438 \u043a\u0443\u0434\u0430 \u0432\u0430\u043c \u0443\u0434\u043e\u0431\u043d\u043e):<\/p>\n<pre><code class=\"powershell\">pip install beautifulsoup4 pip install requests pip install fake-useragent<\/code><\/pre>\n<p>\u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0438\u0440\u0443\u0435\u043c \u043c\u043e\u0434\u0443\u043b\u0438 \u0432 \u043d\u0430\u0448 \u0437\u0430\u0440\u0430\u043d\u0435\u0435 \u0441\u043e\u0437\u0434\u0430\u043d\u043d\u044b\u0439 \u043f\u0440\u043e\u0435\u043a\u0442, \u0442.\u0435. \u0444\u0430\u0439\u043b \u0441 \u0440\u0430\u0441\u0448\u0438\u0440\u0435\u043d\u0438\u0435\u043c py.<\/p>\n<pre><code class=\"python\">from bs4 import BeautifulSoup import random import json import requests import datetime from fake_useragent import UserAgent<\/code><\/pre>\n<p>\u0421\u043e\u0437\u0434\u0430\u0435\u043c \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u0443\u044e \u0441 \u043c\u043e\u0434\u0443\u043b\u0435\u043c fake_useragent, \u0447\u0442\u043e\u0431\u044b \u043c\u044b \u043c\u043e\u0433\u043b\u0438 \u043f\u043e\u0442\u043e\u043c \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0434\u043b\u044f \u0433\u0435\u043d\u0435\u0440\u0430\u0446\u0438\u0438 user-agent:  <\/p>\n<pre><code class=\"python\">ua = UserAgent()<\/code><\/pre>\n<p>\u041e\u043f\u0440\u0435\u0434\u0435\u043b\u044f\u0435\u043c \u0437\u0430\u0433\u043e\u043b\u043e\u0432\u043a\u0438 (\u0434\u043b\u044f \u0442\u043e\u0433\u043e \u0447\u0442\u043e\u0431\u044b \u0441\u0435\u0440\u0432\u0435\u0440 \u0441\u0430\u0439\u0442\u0430 \u043c\u043e\u0433 \u043f\u043e\u043d\u044f\u0442\u044c, \u043a\u0430\u043a \u043e\u043d \u0434\u043e\u043b\u0436\u0435\u043d \u043e\u0442\u043f\u0440\u0430\u0432\u0438\u0442\u044c \u043e\u0442\u0432\u0435\u0442, \u0430 \u0442\u0430\u043a\u0436\u0435 \u043f\u043e\u043c\u043e\u0433\u0430\u0435\u0442 \u0441\u0435\u0440\u0432\u0435\u0440\u0443 \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0438\u0442\u044c \u043e\u0442\u043f\u0440\u0430\u0432\u0438\u0442\u0435\u043b\u044f \u0437\u0430\u043f\u0440\u043e\u0441\u0430)<\/p>\n<pre><code class=\"python\">headers = {     'accept': 'application\/json, text\/plain, *\/*',     'user-Agent': ua.google, }<\/code><\/pre>\n<p>\u0421\u043e\u0437\u0434\u0430\u0435\u043c \u0441\u043b\u043e\u0432\u0430\u0440\u044c, \u0433\u0434\u0435 \u0431\u0443\u0434\u0443\u0442 \u0445\u0440\u0430\u043d\u0438\u0442\u044c\u0441\u044f \u043d\u0430\u0437\u0432\u0430\u043d\u0438\u0435 \u0438 \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043a\u0430\u0436\u0434\u0443\u044e \u0441\u0442\u0430\u0442\u044c\u044e:<\/p>\n<pre><code>article_dict = {}<\/code><\/pre>\n<p>\u0421\u043e\u0437\u0434\u0430\u0435\u043c \u0446\u0438\u043a\u043b \u0434\u043b\u044f \u0441\u0431\u043e\u0440\u0430 \u0441\u043e \u0432\u0441\u0435\u0445 \u0441\u0442\u0440\u0430\u043d\u0438\u0446, \u0430 \u043d\u0435 \u0441 \u043e\u0434\u043d\u043e\u0439 (\u0441 1 \u043f\u043e 3, \u0442.\u043a. \u0441\u0442\u0440\u0430\u043d\u0438\u0446 \u0441 \u0441\u0441\u044b\u043b\u043a\u0430\u043c\u0438 \u0432 \u0434\u0435\u043d\u044c, \u043a\u0430\u043a \u044f \u043f\u043e\u043d\u0438\u043c\u0430\u044e \u0432\u0441\u0435\u0433\u043e 3).<\/p>\n<pre><code>for i in range(1, 4):<\/code><\/pre>\n<p>\u0423\u043a\u0430\u0437\u044b\u0432\u0430\u0435\u043c url c \u0444\u043e\u0440\u043c\u0430\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 \u043a\u043e\u0434\u0430, \u0433\u0434\u0435 i &#8212; \u043d\u043e\u043c\u0435\u0440 \u0441\u0442\u0440\u0430\u043d\u0438\u0446\u044b, \u043a\u043e\u0442\u043e\u0440\u043e\u0435 \u0432\u0441\u0442\u0430\u0432\u043b\u044f\u0442\u044c\u0441\u044f \u043f\u0440\u0438 \u043a\u0430\u0436\u0434\u043e\u043c \u043f\u0440\u043e\u0445\u043e\u0434\u0435 \u0446\u0438\u043a\u043b\u0430.<\/p>\n<pre><code class=\"python\">url = f'https:\/\/habr.com\/ru\/top\/daily\/page{i}\/'<\/code><\/pre>\n<p>\u041e\u0442\u043f\u0440\u0430\u0432\u043b\u044f\u0435\u043c get \u0437\u0430\u043f\u0440\u043e\u0441 \u043d\u0430 \u0441\u0430\u0439\u0442, \u0443\u043a\u0430\u0437\u044b\u0432\u0430\u044f \u0432 \u043f\u0435\u0440\u0432\u043e\u043c \u0430\u0440\u0433\u0443\u043c\u0435\u043d\u0442\u0435 &#8212; \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u0443\u044e \u0441 url \u0441\u0430\u0439\u0442\u0430, \u0432\u043e-\u0432\u0442\u043e\u0440\u043e\u043c \u0437\u0430\u0433\u043e\u043b\u043e\u0432\u043a\u0438. \u0410\u0442\u0440\u0438\u0431\u0443\u0442 text, \u043d\u0443\u0436\u0435\u043d \u0447\u0442\u043e\u0431\u044b \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0442\u0435\u043a\u0441\u0442\u043e\u0432\u043e\u0435 \u0441\u043e\u0434\u0435\u0440\u0436\u0430\u043d\u0438\u0435\u043c html \u0441\u0442\u0440\u0430\u043d\u0438\u0446\u044b.<\/p>\n<pre><code class=\"python\">req = requests.get(url, headers=headers).text<\/code><\/pre>\n<p>\u0422\u0435\u043f\u0435\u0440\u044c \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e BeautifulSoup \u0441\u043e\u0431\u0435\u0440\u0435\u043c \u0432\u0435\u0441\u044c html \u043a\u043e\u0434 \u0441\u0442\u0440\u0430\u043d\u0438\u0446\u044b.<\/p>\n<pre><code class=\"python\">soup = BeautifulSoup(req, 'lxml')<\/code><\/pre>\n<p>\u0415\u0441\u043b\u0438 \u043f\u043e\u043f\u0440\u043e\u0431\u043e\u0432\u0430\u0442\u044c \u0432\u044b\u0432\u0435\u0441\u0442\u0438 \u0442\u0430\u043a\u043e\u0439 \u043a\u043e\u0434 \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e print(soup), \u0432\u044b\u0432\u0435\u0434\u0435\u0442\u0441\u044f \u0432\u0435\u0441\u044c html \u043a\u043e\u0434 \u0441\u0442\u0440\u0430\u043d\u0438\u0446\u044b.<\/p>\n<p>\u0414\u0430\u043b\u0435\u0435, \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044f \u043d\u0430\u0448 &#171;soup&#187; \u0441\u043e\u0437\u0434\u0430\u043d\u043d\u044b\u0439 \u0432 \u043f\u0440\u043e\u0448\u043b\u043e\u043c \u0448\u0430\u0433\u0435, \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u043c\u0435\u0442\u043e\u0434\u0430 find_all \u0441\u043e\u0431\u0438\u0440\u0430\u0435\u043c \u0432\u0441\u0435 \u0441\u0441\u044b\u043b\u043a\u0438 \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u0442\u0435\u0433\u0430 &#171;a&#187; \u0432 \u043f\u0435\u0440\u0432\u043e\u043c \u0430\u0440\u0433\u0443\u043c\u0435\u043d\u0442\u0435, \u0432\u043e-\u0432\u0442\u043e\u0440\u043e\u043c, \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e F12, \u0438\u0449\u0435\u043c \u043a\u043b\u0430\u0441\u0441 \u0443 \u0432\u0441\u0435\u0445 \u0441\u0441\u044b\u043b\u043e\u043a \u043d\u0430\u0448\u0438\u0445 \u0441\u0442\u0430\u0442\u0435\u0439, \u043a\u0430\u043a \u043c\u044b \u0432\u0438\u0434\u0438\u043c \u044d\u0442\u043e &#8212; tm-article-snippet__title-link.<\/p>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/3ea\/6be\/e7f\/3ea6bee7f24865d04aa634b568982491.png\" alt=\"a - \u044d\u0442\u043e \u0442\u0435\u0433, \u043f\u043e\u0441\u043b\u0435 \u0442\u043e\u0447\u043a\u0438 - \u043a\u043b\u0430\u0441\u0441 \u0442\u0435\u0433\u0430.\" title=\"a - \u044d\u0442\u043e \u0442\u0435\u0433, \u043f\u043e\u0441\u043b\u0435 \u0442\u043e\u0447\u043a\u0438 - \u043a\u043b\u0430\u0441\u0441 \u0442\u0435\u0433\u0430.\" width=\"736\" height=\"181\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/3ea\/6be\/e7f\/3ea6bee7f24865d04aa634b568982491.png\"\/><figcaption>a &#8212; \u044d\u0442\u043e \u0442\u0435\u0433, \u043f\u043e\u0441\u043b\u0435 \u0442\u043e\u0447\u043a\u0438 &#8212; \u043a\u043b\u0430\u0441\u0441 \u0442\u0435\u0433\u0430.<\/figcaption><\/figure>\n<pre><code class=\"python\">all_hrefs_articles = soup.find_all('a', class_='tm-article-snippet__title-link')<\/code><\/pre>\n<p>\u041a\u043b\u0430\u0441\u0441 \u0443\u043a\u0430\u0437\u044b\u0432\u0430\u0435\u0442\u0441\u044f \u0441 \u043d\u0438\u0436\u043d\u0438\u043c \u043f\u043e\u0434\u0447\u0435\u0440\u043a\u0438\u0432\u0430\u043d\u0438\u0435\u043c, \u0442.\u043a. \u044d\u0442\u043e \u043a\u043b\u044e\u0447\u0435\u0432\u043e\u0435 (\u0437\u0430\u0440\u0435\u0437\u0435\u0440\u0432\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u043e\u0435) \u0441\u043b\u043e\u0432\u043e \u0432 Python.<\/p>\n<p>\u0421\u043e\u0437\u0434\u0430\u0435\u043c \u0435\u0449\u0435 \u043e\u0434\u0438\u043d \u0446\u0438\u043a\u043b, \u0433\u0434\u0435 \u043c\u044b \u0431\u0443\u0434\u0435\u043c \u043f\u0440\u043e\u0445\u043e\u0434\u0438\u0442\u044c\u0441\u044f \u043f\u043e \u0432\u0441\u0435\u043c \u0441\u0442\u0430\u0442\u044c\u044f\u043c \u0441\u043e\u0431\u0440\u0430\u043d\u043d\u044b\u0445 \u0432 \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u043e\u0439 all_hrefs_articles.<\/p>\n<p>\u0412 \u0442\u0435\u0433\u0435 &#171;a&#187; \u0441 \u043a\u043b\u0430\u0441\u0441\u043e\u043c &#171;tm-article-snippet__title-link&#187; \u043d\u0430\u0445\u043e\u0434\u0438\u0442\u0441\u044f \u0435\u0449\u0435 \u043e\u0434\u0438\u043d \u0442\u0435\u0433 &#171;span&#187; c \u043d\u0430\u0448\u0438\u043c\u0438 \u0438\u043c\u0435\u043d\u0430\u043c\u0438 \u0441\u0441\u044b\u043b\u043e\u043a, \u043f\u043e\u043b\u0443\u0447\u0438\u043c \u0435\u0433\u043e \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u043c\u0435\u0442\u043e\u0434\u0430 find.<\/p>\n<pre><code class=\"python\">article_name = article.find('span').text # \u0441\u043e\u0431\u0438\u0440\u0430\u0435\u043c \u043d\u0430\u0437\u0432\u0430\u043d\u0438\u044f \u0441\u0442\u0430\u0442\u0435\u0439<\/code><\/pre>\n<p>\u0422\u0435\u043f\u0435\u0440\u044c \u043f\u043e\u043b\u0443\u0447\u0438\u043c \u0441\u0441\u044b\u043b\u043a\u0443 \u043d\u0430 \u0441\u0442\u0430\u0442\u044c\u044e, \u0443\u043a\u0430\u0437\u044b\u0432\u0430\u0435\u043c \u0447\u0442\u043e \u044d\u0442\u043e f \u0441\u0442\u0440\u043e\u043a\u0430, \u0438 \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e get \u0437\u0430\u043f\u0440\u043e\u0441\u0430 \u0432 \u0441\u043a\u043e\u0431\u043e\u0447\u043a\u0430\u0445 \u043f\u043e\u043b\u0443\u0447\u0430\u0435\u043c \u0430\u0442\u0440\u0438\u0431\u0443\u0442 &#171;href&#187; &#8212; \u043e\u0441\u043d\u043e\u0432\u043d\u043e\u0439 \u0430\u0442\u0440\u0438\u0431\u0443\u0442 \u0442\u0435\u0433\u0430 &#171;a&#187;.<\/p>\n<pre><code class=\"python\">article_link = f'https:\/\/habr.com{article.get(\"href\")}'<\/code><\/pre>\n<p>\u041f\u043e\u043b\u0443\u0447\u0430\u0435\u0442\u0441\u044f \u0441\u0441\u044b\u043b\u043a\u0430, \u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440: &#171;https:\/\/habr.com\/ru\/company\/tinkoff\/blog\/715604\/&#187;<\/p>\n<p>\u0422\u0435\u043f\u0435\u0440\u044c \u0443\u043a\u0430\u0437\u044b\u0432\u0430\u0435\u043c \u043f\u0430\u0440\u0443 \u043a\u043b\u044e\u0447 &#8212; \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u0434\u043b\u044f \u043d\u0430\u0437\u0432\u0430\u043d\u0438\u044f \u0438 \u0441\u0441\u044b\u043b\u043a\u0443 \u043d\u0430 \u0441\u0442\u0430\u0442\u044c\u044e (\u0434\u043b\u044f \u043d\u0430\u0448\u0435\u0433\u043e \u0441\u043b\u043e\u0432\u0430\u0440\u044f):<\/p>\n<pre><code class=\"python\">article_dict[article_name] = article_link<\/code><\/pre>\n<p><strong>\u0412\u044b\u0445\u043e\u0434\u0438\u043c \u0438\u0437 \u043e\u0431\u043e\u0438\u0445 \u0446\u0438\u043a\u043b\u043e\u0432.<\/strong> \u0421 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u043a\u043e\u043d\u0441\u0442\u0440\u0443\u043a\u0446\u0438\u0438 &#171;with open&#187; \u0441\u043e\u0437\u0434\u0430\u0435\u043c \u0444\u0430\u0439\u043b articles_ + \u0434\u0430\u0442\u0430 \u0438 \u0432\u0440\u0435\u043c\u044f \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u044f \u0444\u0430\u0439\u043b\u0430 \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u043c\u043e\u0434\u0443\u043b\u044f datetime, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043c\u044b \u0438\u043c\u043f\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u043b\u0438, \u0444\u0430\u0439\u043b \u0441\u043e\u0437\u0434\u0430\u0435\u043c \u0441 \u0440\u0430\u0441\u0448\u0438\u0440\u0435\u043d\u0438\u0435\u043c .json (\u043d\u0443 \u043c\u043d\u0435 \u0442\u0430\u043a \u0443\u0434\u043e\u0431\u043d\u0435\u0435), \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u0435 \u043c\u044b \u0443\u043a\u0430\u0437\u044b\u0432\u0430\u0435\u043c &#8216;w&#8217;, \u0447\u0442\u043e \u043e\u0437\u043d\u0430\u0447\u0430\u0435\u0442, \u0447\u0442\u043e \u043d\u0443\u0436\u043d\u043e \u0441\u043e\u0437\u0434\u0430\u0442\u044c \u0444\u0430\u0439\u043b \u0441 \u0442\u0430\u043a\u0438\u043c-\u0442\u043e \u043d\u0430\u0437\u0432\u0430\u043d\u0438\u0435\u043c \u0438 \u0432\u043f\u0438\u0441\u0430\u0442\u044c \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0439 \u043a\u043e\u0434, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043c\u044b \u0443\u043a\u0430\u0436\u0435\u043c \u0432\u043d\u0443\u0442\u0440\u0438 \u0444\u0430\u0439\u043b\u0430, \u0442\u0430\u043a\u0436\u0435 \u0443\u043a\u0430\u0437\u044b\u0432\u0430\u0435\u043c \u043a\u043e\u0434\u0438\u0440\u043e\u0432\u043a\u0443 &#187; encoding =&#8217;utf-8&#8242; &#171;, \u0447\u0442\u043e\u0431\u044b \u0444\u0430\u0439\u043b \u043c\u043e\u0433 \u043e\u0442\u043e\u0431\u0440\u0430\u0437\u0438\u0442\u044c \u0440\u0443\u0441\u0441\u043a\u0438\u0435 \u0441\u0438\u043c\u0432\u043e\u043b\u044b.<\/p>\n<pre><code class=\"python\">with open(f\"articles_{datetime.datetime.now().strftime('%d_%m_%Y')}.json\", \"w\", encoding='utf-8') as f: <\/code><\/pre>\n<p>\u0421\u043e\u0437\u0434\u0430\u0435\u043c \u043a\u043e\u043d\u0441\u0442\u0440\u0443\u043a\u0446\u0438\u044e try, except (\u0435\u0441\u043b\u0438 \u043d\u0435\u0442 \u043e\u0448\u0438\u0431\u043e\u043a \u043f\u0440\u0438 \u043f\u0430\u0440\u0441\u0438\u043d\u0433\u0435, \u0432\u044b\u0432\u043e\u0434\u0438\u0442\u0441\u044f try, \u0435\u0441\u043b\u0438 \u0432\u044b\u0432\u043e\u0434\u0438\u0442\u0441\u044f \u043e\u0448\u0438\u0431\u043a\u0430 \u043f\u0440\u0438 \u043f\u0430\u0440\u0441\u0438\u043d\u0433\u0435 => except)<\/p>\n<pre><code class=\"python\">try:  except:   <\/code><\/pre>\n<p>\u0412 try, \u043c\u044b &#171;\u0433\u043e\u0432\u043e\u0440\u0438\u043c&#187;, \u0447\u0442\u043e\u0431\u044b \u0432 json \u0444\u0430\u0439\u043b \u043e\u0442\u043f\u0440\u0430\u0432\u043b\u044f\u043b\u0438\u0441\u044c \u0434\u0430\u043d\u043d\u044b\u0435, 1 &#8212; \u0441\u043b\u043e\u0432\u0430\u0440\u044c \u0441 \u043d\u0430\u0448\u0438\u043c\u0438 \u0441\u0442\u0430\u0442\u044c\u044f\u043c\u0438 , 2 &#8212; \u0438\u043c\u044f \u0444\u0430\u0439\u043b\u0430, \u043a\u0443\u0434\u0430 \u043e\u0442\u043f\u0440\u0430\u0432\u043b\u044f\u0442\u044c \u0434\u0430\u043d\u043d\u044b\u0435 (\u0432 \u043e\u0442\u043a\u0440\u044b\u0442\u0438\u0438 \u0444\u0430\u0439\u043b\u0430 \u043c\u044b \u0443\u043a\u0430\u0437\u0430\u043b\u0438 \u0432 \u043a\u043e\u043d\u0446\u0435 \u0435\u0433\u043e \u043a\u0430\u043a f, \u0447\u0442\u043e\u0431\u044b \u0441 \u043d\u0438\u043c \u043c\u043e\u0436\u043d\u043e \u0431\u044b\u043b\u043e \u0440\u0430\u0431\u043e\u0442\u0430\u0442\u044c), 3 &#8212; \u043e\u0442\u0441\u0442\u0443\u043f\u044b (\u044f \u0441\u0434\u0435\u043b\u0430\u043b 4 \u0434\u043b\u044f \u0443\u0434\u043e\u0431\u0441\u0442\u0432\u0430 \u0447\u0442\u0435\u043d\u0438\u044f, \u043c\u043e\u0436\u043d\u043e \u0443\u043a\u0430\u0437\u0430\u0442\u044c \u0441\u0432\u043e\u0435), 4 &#8212; \u044d\u043a\u0440\u0430\u043d\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 ASCII \u0441\u0438\u043c\u0432\u043e\u043b\u043e\u0432, \u0438 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u0439 \u0441\u0442\u0440\u043e\u043a\u043e\u0439 \u0432\u044b\u0432\u043e\u0434, \u0447\u0442\u043e \u0441\u0442\u0430\u0442\u044c\u0438 \u0443\u0441\u043f\u0435\u0448\u043d\u043e \u0431\u044b\u043b\u0438 \u043f\u043e\u043b\u0443\u0447\u0435\u043d\u044b.<\/p>\n<pre><code class=\"python\">print('\u0421\u0442\u0430\u0442\u044c\u0438 \u0431\u044b\u043b\u0438 \u0443\u0441\u043f\u0435\u0448\u043d\u043e \u043f\u043e\u043b\u0443\u0447\u0435\u043d\u044b')<\/code><\/pre>\n<p>\u0412 except, \u043c\u044b \u0432\u044b\u0432\u043e\u0434\u0438\u043c, \u0447\u0442\u043e \u0441\u0442\u0430\u0442\u044c\u0438 \u043d\u0435 \u0443\u0434\u0430\u043b\u043e\u0441\u044c \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0438 \u043d\u0443\u0436\u043d\u043e \u0438\u0441\u043a\u0430\u0442\u044c \u043f\u0440\u043e\u0431\u043b\u0435\u043c\u044b \u0432 \u043a\u043e\u0434\u0435.<\/p>\n<pre><code class=\"python\">print('\u0421\u0442\u0430\u0442\u044c\u0438 \u0431\u044b\u043b\u0438 \u0443\u0441\u043f\u0435\u0448\u043d\u043e \u043f\u043e\u043b\u0443\u0447\u0435\u043d\u044b')<\/code><\/pre>\n<p>\u0412 \u043a\u043e\u043d\u0435\u0447\u043d\u043e\u043c \u0438\u0442\u043e\u0433\u0435, \u0434\u043e\u043b\u0436\u043d\u043e \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c\u0441\u044f \u0447\u0442\u043e-\u0442\u043e \u043f\u043e\u0445\u043e\u0436\u0435\u0435:<\/p>\n<pre><code class=\"python\">from bs4 import BeautifulSoup import random import json import requests import datetime from fake_useragent import UserAgent  ua = UserAgent()  headers = {     'accept': 'application\/json, text\/plain, *\/*',     'user-Agent': ua.google, }  article_dict = {}  for i in range(1, 4):     url = f'https:\/\/habr.com\/ru\/top\/daily\/page{i}\/'      req = requests.get(url, headers=headers).text      soup = BeautifulSoup(req, 'lxml')     all_hrefs_articles = soup.find_all('a', class_='tm-article-snippet__title-link') # \u043f\u043e\u043b\u0443\u0447\u0430\u0435\u043c \u0441\u0442\u0430\u0442\u044c\u0438      for article in all_hrefs_articles: # \u043f\u0440\u043e\u0445\u043e\u0434\u0438\u043c\u0441\u044f \u043f\u043e \u0441\u0442\u0430\u0442\u044c\u044f\u043c         article_name = article.find('span').text # \u0441\u043e\u0431\u0438\u0440\u0430\u0435\u043c \u043d\u0430\u0437\u0432\u0430\u043d\u0438\u044f \u0441\u0442\u0430\u0442\u0435\u0439         article_link = f'https:\/\/habr.com{article.get(\"href\")}' # \u0441\u0441\u044b\u043b\u043a\u0438 \u043d\u0430 \u0441\u0442\u0430\u0442\u044c\u0438         article_dict[article_name] = article_link  with open(f\"articles_{datetime.datetime.now().strftime('%d_%m_%Y')}.json\", \"w\", encoding='utf-8') as f:      try:             json.dump(article_dict, f, indent=4, ensure_ascii=False)         print('\u0421\u0442\u0430\u0442\u044c\u0438 \u0431\u044b\u043b\u0438 \u0443\u0441\u043f\u0435\u0448\u043d\u043e \u043f\u043e\u043b\u0443\u0447\u0435\u043d\u044b')     except:         print('\u0421\u0442\u0430\u0442\u044c\u0438 \u043d\u0435 \u0443\u0434\u0430\u043b\u043e\u0441\u044c \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c')<\/code><\/pre>\n<\/p>\n<\/div>\n<\/div>\n<\/div>\n<p> <!----> <!----><\/div>\n<p> <!----> <!----><br \/> \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b \u0441\u0442\u0430\u0442\u044c\u0438 <a href=\"https:\/\/habr.com\/ru\/post\/717556\/\"> https:\/\/habr.com\/ru\/post\/717556\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<div><\/div>\n<div id=\"post-content-body\">\n<div>\n<div class=\"article-formatted-body article-formatted-body article-formatted-body_version-2\">\n<div xmlns=\"http:\/\/www.w3.org\/1999\/xhtml\">\n<p>\u0412\u043e-\u043f\u0435\u0440\u0432\u044b\u0445, \u044f \u043d\u0430\u0447\u0438\u043d\u0430\u044e\u0449\u0438\u0439 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0441\u0442.<\/p>\n<p>\u0412\u043e-\u0432\u0442\u043e\u0440\u044b\u0445 \u0434\u0430\u0432\u0430\u0439\u0442\u0435 \u043f\u0440\u0438\u0441\u0442\u0443\u043f\u0438\u043c.<\/p>\n<p>\u041d\u0443\u0436\u043d\u044b\u0435 \u043d\u0430\u043c \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u0438 (\u0432\u0441\u0442\u0430\u0432\u043b\u044f\u0435\u043c \u0432 \u043a\u043e\u043c\u0430\u043d\u0434\u043d\u0443\u044e \u0441\u0442\u0440\u043e\u043a\u0443, \u043b\u0438\u0431\u043e \u0432 \u0442\u0435\u0440\u043c\u0438\u043d\u0430\u043b\u0435 \u0438\u043b\u0438 \u043a\u0443\u0434\u0430 \u0432\u0430\u043c \u0443\u0434\u043e\u0431\u043d\u043e):<\/p>\n<pre><code class=\"powershell\">pip install beautifulsoup4 pip install requests pip install fake-useragent<\/code><\/pre>\n<p>\u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0438\u0440\u0443\u0435\u043c \u043c\u043e\u0434\u0443\u043b\u0438 \u0432 \u043d\u0430\u0448 \u0437\u0430\u0440\u0430\u043d\u0435\u0435 \u0441\u043e\u0437\u0434\u0430\u043d\u043d\u044b\u0439 \u043f\u0440\u043e\u0435\u043a\u0442, \u0442.\u0435. \u0444\u0430\u0439\u043b \u0441 \u0440\u0430\u0441\u0448\u0438\u0440\u0435\u043d\u0438\u0435\u043c py.<\/p>\n<pre><code class=\"python\">from bs4 import BeautifulSoup import random import json import requests import datetime from fake_useragent import UserAgent<\/code><\/pre>\n<p>\u0421\u043e\u0437\u0434\u0430\u0435\u043c \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u0443\u044e \u0441 \u043c\u043e\u0434\u0443\u043b\u0435\u043c fake_useragent, \u0447\u0442\u043e\u0431\u044b \u043c\u044b \u043c\u043e\u0433\u043b\u0438 \u043f\u043e\u0442\u043e\u043c \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0434\u043b\u044f \u0433\u0435\u043d\u0435\u0440\u0430\u0446\u0438\u0438 user-agent:  <\/p>\n<pre><code class=\"python\">ua = UserAgent()<\/code><\/pre>\n<p>\u041e\u043f\u0440\u0435\u0434\u0435\u043b\u044f\u0435\u043c \u0437\u0430\u0433\u043e\u043b\u043e\u0432\u043a\u0438 (\u0434\u043b\u044f \u0442\u043e\u0433\u043e \u0447\u0442\u043e\u0431\u044b \u0441\u0435\u0440\u0432\u0435\u0440 \u0441\u0430\u0439\u0442\u0430 \u043c\u043e\u0433 \u043f\u043e\u043d\u044f\u0442\u044c, \u043a\u0430\u043a \u043e\u043d \u0434\u043e\u043b\u0436\u0435\u043d \u043e\u0442\u043f\u0440\u0430\u0432\u0438\u0442\u044c \u043e\u0442\u0432\u0435\u0442, \u0430 \u0442\u0430\u043a\u0436\u0435 \u043f\u043e\u043c\u043e\u0433\u0430\u0435\u0442 \u0441\u0435\u0440\u0432\u0435\u0440\u0443 \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0438\u0442\u044c \u043e\u0442\u043f\u0440\u0430\u0432\u0438\u0442\u0435\u043b\u044f \u0437\u0430\u043f\u0440\u043e\u0441\u0430)<\/p>\n<pre><code class=\"python\">headers = {     'accept': 'application\/json, text\/plain, *\/*',     'user-Agent': ua.google, }<\/code><\/pre>\n<p>\u0421\u043e\u0437\u0434\u0430\u0435\u043c \u0441\u043b\u043e\u0432\u0430\u0440\u044c, \u0433\u0434\u0435 \u0431\u0443\u0434\u0443\u0442 \u0445\u0440\u0430\u043d\u0438\u0442\u044c\u0441\u044f \u043d\u0430\u0437\u0432\u0430\u043d\u0438\u0435 \u0438 \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043a\u0430\u0436\u0434\u0443\u044e \u0441\u0442\u0430\u0442\u044c\u044e:<\/p>\n<pre><code>article_dict = {}<\/code><\/pre>\n<p>\u0421\u043e\u0437\u0434\u0430\u0435\u043c \u0446\u0438\u043a\u043b \u0434\u043b\u044f \u0441\u0431\u043e\u0440\u0430 \u0441\u043e \u0432\u0441\u0435\u0445 \u0441\u0442\u0440\u0430\u043d\u0438\u0446, \u0430 \u043d\u0435 \u0441 \u043e\u0434\u043d\u043e\u0439 (\u0441 1 \u043f\u043e 3, \u0442.\u043a. \u0441\u0442\u0440\u0430\u043d\u0438\u0446 \u0441 \u0441\u0441\u044b\u043b\u043a\u0430\u043c\u0438 \u0432 \u0434\u0435\u043d\u044c, \u043a\u0430\u043a \u044f \u043f\u043e\u043d\u0438\u043c\u0430\u044e \u0432\u0441\u0435\u0433\u043e 3).<\/p>\n<pre><code>for i in range(1, 4):<\/code><\/pre>\n<p>\u0423\u043a\u0430\u0437\u044b\u0432\u0430\u0435\u043c url c \u0444\u043e\u0440\u043c\u0430\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 \u043a\u043e\u0434\u0430, \u0433\u0434\u0435 i &#8212; \u043d\u043e\u043c\u0435\u0440 \u0441\u0442\u0440\u0430\u043d\u0438\u0446\u044b, \u043a\u043e\u0442\u043e\u0440\u043e\u0435 \u0432\u0441\u0442\u0430\u0432\u043b\u044f\u0442\u044c\u0441\u044f \u043f\u0440\u0438 \u043a\u0430\u0436\u0434\u043e\u043c \u043f\u0440\u043e\u0445\u043e\u0434\u0435 \u0446\u0438\u043a\u043b\u0430.<\/p>\n<pre><code class=\"python\">url = f'https:\/\/habr.com\/ru\/top\/daily\/page{i}\/'<\/code><\/pre>\n<p>\u041e\u0442\u043f\u0440\u0430\u0432\u043b\u044f\u0435\u043c get \u0437\u0430\u043f\u0440\u043e\u0441 \u043d\u0430 \u0441\u0430\u0439\u0442, \u0443\u043a\u0430\u0437\u044b\u0432\u0430\u044f \u0432 \u043f\u0435\u0440\u0432\u043e\u043c \u0430\u0440\u0433\u0443\u043c\u0435\u043d\u0442\u0435 &#8212; \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u0443\u044e \u0441 url \u0441\u0430\u0439\u0442\u0430, \u0432\u043e-\u0432\u0442\u043e\u0440\u043e\u043c \u0437\u0430\u0433\u043e\u043b\u043e\u0432\u043a\u0438. \u0410\u0442\u0440\u0438\u0431\u0443\u0442 text, \u043d\u0443\u0436\u0435\u043d \u0447\u0442\u043e\u0431\u044b \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0442\u0435\u043a\u0441\u0442\u043e\u0432\u043e\u0435 \u0441\u043e\u0434\u0435\u0440\u0436\u0430\u043d\u0438\u0435\u043c html \u0441\u0442\u0440\u0430\u043d\u0438\u0446\u044b.<\/p>\n<pre><code class=\"python\">req = requests.get(url, headers=headers).text<\/code><\/pre>\n<p>\u0422\u0435\u043f\u0435\u0440\u044c \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e BeautifulSoup \u0441\u043e\u0431\u0435\u0440\u0435\u043c \u0432\u0435\u0441\u044c html \u043a\u043e\u0434 \u0441\u0442\u0440\u0430\u043d\u0438\u0446\u044b.<\/p>\n<pre><code class=\"python\">soup = BeautifulSoup(req, 'lxml')<\/code><\/pre>\n<p>\u0415\u0441\u043b\u0438 \u043f\u043e\u043f\u0440\u043e\u0431\u043e\u0432\u0430\u0442\u044c \u0432\u044b\u0432\u0435\u0441\u0442\u0438 \u0442\u0430\u043a\u043e\u0439 \u043a\u043e\u0434 \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e print(soup), \u0432\u044b\u0432\u0435\u0434\u0435\u0442\u0441\u044f \u0432\u0435\u0441\u044c html \u043a\u043e\u0434 \u0441\u0442\u0440\u0430\u043d\u0438\u0446\u044b.<\/p>\n<p>\u0414\u0430\u043b\u0435\u0435, \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044f \u043d\u0430\u0448 &#171;soup&#187; \u0441\u043e\u0437\u0434\u0430\u043d\u043d\u044b\u0439 \u0432 \u043f\u0440\u043e\u0448\u043b\u043e\u043c \u0448\u0430\u0433\u0435, \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u043c\u0435\u0442\u043e\u0434\u0430 find_all \u0441\u043e\u0431\u0438\u0440\u0430\u0435\u043c \u0432\u0441\u0435 \u0441\u0441\u044b\u043b\u043a\u0438 \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u0442\u0435\u0433\u0430 &#171;a&#187; \u0432 \u043f\u0435\u0440\u0432\u043e\u043c \u0430\u0440\u0433\u0443\u043c\u0435\u043d\u0442\u0435, \u0432\u043e-\u0432\u0442\u043e\u0440\u043e\u043c, \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e F12, \u0438\u0449\u0435\u043c \u043a\u043b\u0430\u0441\u0441 \u0443 \u0432\u0441\u0435\u0445 \u0441\u0441\u044b\u043b\u043e\u043a \u043d\u0430\u0448\u0438\u0445 \u0441\u0442\u0430\u0442\u0435\u0439, \u043a\u0430\u043a \u043c\u044b \u0432\u0438\u0434\u0438\u043c \u044d\u0442\u043e &#8212; tm-article-snippet__title-link.<\/p>\n<figure class=\"full-width\"><figcaption>a &#8212; \u044d\u0442\u043e \u0442\u0435\u0433, \u043f\u043e\u0441\u043b\u0435 \u0442\u043e\u0447\u043a\u0438 &#8212; \u043a\u043b\u0430\u0441\u0441 \u0442\u0435\u0433\u0430.<\/figcaption><\/figure>\n<pre><code class=\"python\">all_hrefs_articles = soup.find_all('a', class_='tm-article-snippet__title-link')<\/code><\/pre>\n<p>\u041a\u043b\u0430\u0441\u0441 \u0443\u043a\u0430\u0437\u044b\u0432\u0430\u0435\u0442\u0441\u044f \u0441 \u043d\u0438\u0436\u043d\u0438\u043c \u043f\u043e\u0434\u0447\u0435\u0440\u043a\u0438\u0432\u0430\u043d\u0438\u0435\u043c, \u0442.\u043a. \u044d\u0442\u043e \u043a\u043b\u044e\u0447\u0435\u0432\u043e\u0435 (\u0437\u0430\u0440\u0435\u0437\u0435\u0440\u0432\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u043e\u0435) \u0441\u043b\u043e\u0432\u043e \u0432 Python.<\/p>\n<p>\u0421\u043e\u0437\u0434\u0430\u0435\u043c \u0435\u0449\u0435 \u043e\u0434\u0438\u043d \u0446\u0438\u043a\u043b, \u0433\u0434\u0435 \u043c\u044b \u0431\u0443\u0434\u0435\u043c \u043f\u0440\u043e\u0445\u043e\u0434\u0438\u0442\u044c\u0441\u044f \u043f\u043e \u0432\u0441\u0435\u043c \u0441\u0442\u0430\u0442\u044c\u044f\u043c \u0441\u043e\u0431\u0440\u0430\u043d\u043d\u044b\u0445 \u0432 \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u043e\u0439 all_hrefs_articles.<\/p>\n<p>\u0412 \u0442\u0435\u0433\u0435 &#171;a&#187; \u0441 \u043a\u043b\u0430\u0441\u0441\u043e\u043c &#171;tm-article-snippet__title-link&#187; \u043d\u0430\u0445\u043e\u0434\u0438\u0442\u0441\u044f \u0435\u0449\u0435 \u043e\u0434\u0438\u043d \u0442\u0435\u0433 &#171;span&#187; c \u043d\u0430\u0448\u0438\u043c\u0438 \u0438\u043c\u0435\u043d\u0430\u043c\u0438 \u0441\u0441\u044b\u043b\u043e\u043a, \u043f\u043e\u043b\u0443\u0447\u0438\u043c \u0435\u0433\u043e \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u043c\u0435\u0442\u043e\u0434\u0430 find.<\/p>\n<pre><code class=\"python\">article_name = article.find('span').text # \u0441\u043e\u0431\u0438\u0440\u0430\u0435\u043c \u043d\u0430\u0437\u0432\u0430\u043d\u0438\u044f \u0441\u0442\u0430\u0442\u0435\u0439<\/code><\/pre>\n<p>\u0422\u0435\u043f\u0435\u0440\u044c \u043f\u043e\u043b\u0443\u0447\u0438\u043c \u0441\u0441\u044b\u043b\u043a\u0443 \u043d\u0430 \u0441\u0442\u0430\u0442\u044c\u044e, \u0443\u043a\u0430\u0437\u044b\u0432\u0430\u0435\u043c \u0447\u0442\u043e \u044d\u0442\u043e f \u0441\u0442\u0440\u043e\u043a\u0430, \u0438 \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e get \u0437\u0430\u043f\u0440\u043e\u0441\u0430 \u0432 \u0441\u043a\u043e\u0431\u043e\u0447\u043a\u0430\u0445 \u043f\u043e\u043b\u0443\u0447\u0430\u0435\u043c \u0430\u0442\u0440\u0438\u0431\u0443\u0442 &#171;href&#187; &#8212; \u043e\u0441\u043d\u043e\u0432\u043d\u043e\u0439 \u0430\u0442\u0440\u0438\u0431\u0443\u0442 \u0442\u0435\u0433\u0430 &#171;a&#187;.<\/p>\n<pre><code class=\"python\">article_link = f'https:\/\/habr.com{article.get(\"href\")}'<\/code><\/pre>\n<p>\u041f\u043e\u043b\u0443\u0447\u0430\u0435\u0442\u0441\u044f \u0441\u0441\u044b\u043b\u043a\u0430, \u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440: &#171;https:\/\/habr.com\/ru\/company\/tinkoff\/blog\/715604\/&#187;<\/p>\n<p>\u0422\u0435\u043f\u0435\u0440\u044c \u0443\u043a\u0430\u0437\u044b\u0432\u0430\u0435\u043c \u043f\u0430\u0440\u0443 \u043a\u043b\u044e\u0447 &#8212; \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u0434\u043b\u044f \u043d\u0430\u0437\u0432\u0430\u043d\u0438\u044f \u0438 \u0441\u0441\u044b\u043b\u043a\u0443 \u043d\u0430 \u0441\u0442\u0430\u0442\u044c\u044e (\u0434\u043b\u044f \u043d\u0430\u0448\u0435\u0433\u043e \u0441\u043b\u043e\u0432\u0430\u0440\u044f):<\/p>\n<pre><code class=\"python\">article_dict[article_name] = article_link<\/code><\/pre>\n<p><strong>\u0412\u044b\u0445\u043e\u0434\u0438\u043c \u0438\u0437 \u043e\u0431\u043e\u0438\u0445 \u0446\u0438\u043a\u043b\u043e\u0432.<\/strong> \u0421 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u043a\u043e\u043d\u0441\u0442\u0440\u0443\u043a\u0446\u0438\u0438 &#171;with open&#187; \u0441\u043e\u0437\u0434\u0430\u0435\u043c \u0444\u0430\u0439\u043b articles_ + \u0434\u0430\u0442\u0430 \u0438 \u0432\u0440\u0435\u043c\u044f \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u044f \u0444\u0430\u0439\u043b\u0430 \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u043c\u043e\u0434\u0443\u043b\u044f datetime, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043c\u044b \u0438\u043c\u043f\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u043b\u0438, \u0444\u0430\u0439\u043b \u0441\u043e\u0437\u0434\u0430\u0435\u043c \u0441 \u0440\u0430\u0441\u0448\u0438\u0440\u0435\u043d\u0438\u0435\u043c .json (\u043d\u0443 \u043c\u043d\u0435 \u0442\u0430\u043a \u0443\u0434\u043e\u0431\u043d\u0435\u0435), \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u0435 \u043c\u044b \u0443\u043a\u0430\u0437\u044b\u0432\u0430\u0435\u043c &#8216;w&#8217;, \u0447\u0442\u043e \u043e\u0437\u043d\u0430\u0447\u0430\u0435\u0442, \u0447\u0442\u043e \u043d\u0443\u0436\u043d\u043e \u0441\u043e\u0437\u0434\u0430\u0442\u044c \u0444\u0430\u0439\u043b \u0441 \u0442\u0430\u043a\u0438\u043c-\u0442\u043e \u043d\u0430\u0437\u0432\u0430\u043d\u0438\u0435\u043c \u0438 \u0432\u043f\u0438\u0441\u0430\u0442\u044c \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0439 \u043a\u043e\u0434, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043c\u044b \u0443\u043a\u0430\u0436\u0435\u043c \u0432\u043d\u0443\u0442\u0440\u0438 \u0444\u0430\u0439\u043b\u0430, \u0442\u0430\u043a\u0436\u0435 \u0443\u043a\u0430\u0437\u044b\u0432\u0430\u0435\u043c \u043a\u043e\u0434\u0438\u0440\u043e\u0432\u043a\u0443 &#187; encoding =&#8217;utf-8&#8242; &#171;, \u0447\u0442\u043e\u0431\u044b \u0444\u0430\u0439\u043b \u043c\u043e\u0433 \u043e\u0442\u043e\u0431\u0440\u0430\u0437\u0438\u0442\u044c \u0440\u0443\u0441\u0441\u043a\u0438\u0435 \u0441\u0438\u043c\u0432\u043e\u043b\u044b.<\/p>\n<pre><code class=\"python\">with open(f\"articles_{datetime.datetime.now().strftime('%d_%m_%Y')}.json\", \"w\", encoding='utf-8') as f: <\/code><\/pre>\n<p>\u0421\u043e\u0437\u0434\u0430\u0435\u043c \u043a\u043e\u043d\u0441\u0442\u0440\u0443\u043a\u0446\u0438\u044e try, except (\u0435\u0441\u043b\u0438 \u043d\u0435\u0442 \u043e\u0448\u0438\u0431\u043e\u043a \u043f\u0440\u0438 \u043f\u0430\u0440\u0441\u0438\u043d\u0433\u0435, \u0432\u044b\u0432\u043e\u0434\u0438\u0442\u0441\u044f try, \u0435\u0441\u043b\u0438 \u0432\u044b\u0432\u043e\u0434\u0438\u0442\u0441\u044f \u043e\u0448\u0438\u0431\u043a\u0430 \u043f\u0440\u0438 \u043f\u0430\u0440\u0441\u0438\u043d\u0433\u0435 => except)<\/p>\n<pre><code class=\"python\">try:  except:   <\/code><\/pre>\n<p>\u0412 try, \u043c\u044b &#171;\u0433\u043e\u0432\u043e\u0440\u0438\u043c&#187;, \u0447\u0442\u043e\u0431\u044b \u0432 json \u0444\u0430\u0439\u043b \u043e\u0442\u043f\u0440\u0430\u0432\u043b\u044f\u043b\u0438\u0441\u044c \u0434\u0430\u043d\u043d\u044b\u0435, 1 &#8212; \u0441\u043b\u043e\u0432\u0430\u0440\u044c \u0441 \u043d\u0430\u0448\u0438\u043c\u0438 \u0441\u0442\u0430\u0442\u044c\u044f\u043c\u0438 , 2 &#8212; \u0438\u043c\u044f \u0444\u0430\u0439\u043b\u0430, \u043a\u0443\u0434\u0430 \u043e\u0442\u043f\u0440\u0430\u0432\u043b\u044f\u0442\u044c \u0434\u0430\u043d\u043d\u044b\u0435 (\u0432 \u043e\u0442\u043a\u0440\u044b\u0442\u0438\u0438 \u0444\u0430\u0439\u043b\u0430 \u043c\u044b \u0443\u043a\u0430\u0437\u0430\u043b\u0438 \u0432 \u043a\u043e\u043d\u0446\u0435 \u0435\u0433\u043e \u043a\u0430\u043a f, \u0447\u0442\u043e\u0431\u044b \u0441 \u043d\u0438\u043c \u043c\u043e\u0436\u043d\u043e \u0431\u044b\u043b\u043e \u0440\u0430\u0431\u043e\u0442\u0430\u0442\u044c), 3 &#8212; \u043e\u0442\u0441\u0442\u0443\u043f\u044b (\u044f \u0441\u0434\u0435\u043b\u0430\u043b 4 \u0434\u043b\u044f \u0443\u0434\u043e\u0431\u0441\u0442\u0432\u0430 \u0447\u0442\u0435\u043d\u0438\u044f, \u043c\u043e\u0436\u043d\u043e \u0443\u043a\u0430\u0437\u0430\u0442\u044c \u0441\u0432\u043e\u0435), 4 &#8212; \u044d\u043a\u0440\u0430\u043d\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 ASCII \u0441\u0438\u043c\u0432\u043e\u043b\u043e\u0432, \u0438 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u0439 \u0441\u0442\u0440\u043e\u043a\u043e\u0439 \u0432\u044b\u0432\u043e\u0434, \u0447\u0442\u043e \u0441\u0442\u0430\u0442\u044c\u0438 \u0443\u0441\u043f\u0435\u0448\u043d\u043e \u0431\u044b\u043b\u0438 \u043f\u043e\u043b\u0443\u0447\u0435\u043d\u044b.<\/p>\n<pre><code class=\"python\">print('\u0421\u0442\u0430\u0442\u044c\u0438 \u0431\u044b\u043b\u0438 \u0443\u0441\u043f\u0435\u0448\u043d\u043e \u043f\u043e\u043b\u0443\u0447\u0435\u043d\u044b')<\/code><\/pre>\n<p>\u0412 except, \u043c\u044b \u0432\u044b\u0432\u043e\u0434\u0438\u043c, \u0447\u0442\u043e \u0441\u0442\u0430\u0442\u044c\u0438 \u043d\u0435 \u0443\u0434\u0430\u043b\u043e\u0441\u044c \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0438 \u043d\u0443\u0436\u043d\u043e \u0438\u0441\u043a\u0430\u0442\u044c \u043f\u0440\u043e\u0431\u043b\u0435\u043c\u044b \u0432 \u043a\u043e\u0434\u0435.<\/p>\n<pre><code class=\"python\">print('\u0421\u0442\u0430\u0442\u044c\u0438 \u0431\u044b\u043b\u0438 \u0443\u0441\u043f\u0435\u0448\u043d\u043e \u043f\u043e\u043b\u0443\u0447\u0435\u043d\u044b')<\/code><\/pre>\n<p>\u0412 \u043a\u043e\u043d\u0435\u0447\u043d\u043e\u043c \u0438\u0442\u043e\u0433\u0435, \u0434\u043e\u043b\u0436\u043d\u043e \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c\u0441\u044f \u0447\u0442\u043e-\u0442\u043e \u043f\u043e\u0445\u043e\u0436\u0435\u0435:<\/p>\n<pre><code class=\"python\">from bs4 import BeautifulSoup import random import json import requests import datetime from fake_useragent import UserAgent  ua = UserAgent()  headers = {     'accept': 'application\/json, text\/plain, *\/*',     'user-Agent': ua.google, }  article_dict = {}  for i in range(1, 4):     url = f'https:\/\/habr.com\/ru\/top\/daily\/page{i}\/'      req = requests.get(url, headers=headers).text      soup = BeautifulSoup(req, 'lxml')     all_hrefs_articles = soup.find_all('a', class_='tm-article-snippet__title-link') # \u043f\u043e\u043b\u0443\u0447\u0430\u0435\u043c \u0441\u0442\u0430\u0442\u044c\u0438      for article in all_hrefs_articles: # \u043f\u0440\u043e\u0445\u043e\u0434\u0438\u043c\u0441\u044f \u043f\u043e \u0441\u0442\u0430\u0442\u044c\u044f\u043c         article_name = article.find('span').text # \u0441\u043e\u0431\u0438\u0440\u0430\u0435\u043c \u043d\u0430\u0437\u0432\u0430\u043d\u0438\u044f \u0441\u0442\u0430\u0442\u0435\u0439         article_link = f'https:\/\/habr.com{article.get(\"href\")}' # \u0441\u0441\u044b\u043b\u043a\u0438 \u043d\u0430 \u0441\u0442\u0430\u0442\u044c\u0438         article_dict[article_name] = article_link  with open(f\"articles_{datetime.datetime.now().strftime('%d_%m_%Y')}.json\", \"w\", encoding='utf-8') as f:      try:             json.dump(article_dict, f, indent=4, ensure_ascii=False)         print('\u0421\u0442\u0430\u0442\u044c\u0438 \u0431\u044b\u043b\u0438 \u0443\u0441\u043f\u0435\u0448\u043d\u043e \u043f\u043e\u043b\u0443\u0447\u0435\u043d\u044b')     except:         print('\u0421\u0442\u0430\u0442\u044c\u0438 \u043d\u0435 \u0443\u0434\u0430\u043b\u043e\u0441\u044c \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c')<\/code><\/pre>\n<\/p>\n<\/div>\n<\/div>\n<\/div>\n<p> <!----> <!----><\/div>\n<p> <!----> <!----><br \/> \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b \u0441\u0442\u0430\u0442\u044c\u0438 <a href=\"https:\/\/habr.com\/ru\/post\/717556\/\"> https:\/\/habr.com\/ru\/post\/717556\/<\/a><br \/><\/br><\/br><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"class_list":["post-345573","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/345573","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=345573"}],"version-history":[{"count":0,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/345573\/revisions"}],"wp:attachment":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=345573"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=345573"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=345573"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}