{"id":312344,"date":"2020-10-31T03:00:15","date_gmt":"2020-10-31T03:00:15","guid":{"rendered":"http:\/\/savepearlharbor.com\/?p=312344"},"modified":"-0001-11-30T00:00:00","modified_gmt":"-0001-11-29T21:00:00","slug":"","status":"publish","type":"post","link":"https:\/\/savepearlharbor.com\/?p=312344","title":{"rendered":"aio api crawler"},"content":{"rendered":"\n<div class=\"post__text post__text-html post__text_v1\" id=\"post-content-body\">\u0412\u0441\u0435\u043c \u043f\u0440\u0438\u043c\u0435\u0440. \u042f \u043d\u0430\u0447\u0430\u043b \u0440\u0430\u0431\u043e\u0442\u0430\u0442\u044c \u043d\u0430\u0434 <a href=\"https:\/\/github.com\/pawnhearts\/aio_api_crawler\" rel=\"nofollow\">\u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u043e\u0439<\/a> \u0434\u043b\u044f \u0432\u044b\u0434\u0435\u0440\u0433\u0438\u0432\u0430\u043d\u0438\u044f \u0434\u0430\u043d\u043d\u044b\u0445 \u0438\u0437 \u0440\u0430\u0437\u043d\u044b\u0445 json api. \u0422\u0430\u043a\u0436\u0435 \u043e\u043d\u0430 \u043c\u043e\u0436\u0435\u0442 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c\u0441\u044f \u0434\u043b\u044f \u0442\u0435\u0441\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f api.<br \/>  <a name=\"habracut\"><\/a><br \/>  \u0410\u043f\u0438\u0448\u043a\u0438 \u043e\u043f\u0438\u0441\u044b\u0432\u0430\u044e\u0442\u0441\u044f \u0432 \u0432\u0438\u0434\u0435 \u043a\u043b\u0430\u0441\u0441\u043e\u0432, \u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440<\/p>\n<pre><code class=\"python\">class Categories(JsonEndpoint):     url = &quot;http:\/\/127.0.0.1:8888\/categories&quot;     params = {&quot;page&quot;: range(100), &quot;language&quot;: &quot;en&quot;}     headers = {&quot;User-Agent&quot;: get_user_agent}     results_key = &quot;*.slug&quot;  categories = Categories()   class Posts(JsonEndpoint):     url = &quot;http:\/\/127.0.0.1:8888\/categories\/{category}\/posts&quot;     params = {&quot;page&quot;: range(100), &quot;language&quot;: &quot;en&quot;}     url_params = {&quot;category&quot;: categories.iter_results()}     results_key = &quot;posts&quot;      async def comments(self, post):         comments = Comments(             self.session,             url_params={&quot;category&quot;: post.url.params[&quot;category&quot;], &quot;id&quot;: post[&quot;id&quot;]},         )         return [comment async for comment in comments]  posts = Posts() <\/code><\/pre>\n<p>  \u0412 params \u0438 url_params \u043c\u043e\u0433\u0443\u0442 \u0431\u044b\u0442\u044c \u0444\u0443\u043d\u043a\u0446\u0438\u0438(\u043a\u0430\u043a \u0437\u0434\u0435\u0441\u044c get_user_agent \u2014 \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u0442 \u0441\u043b\u0443\u0447\u0430\u0439\u043d\u044b\u0439 useragent), range, \u0438\u0442\u0435\u0440\u0430\u0442\u043e\u0440\u044b, awaitable \u0438 \u0430\u0441\u0438\u043d\u0445\u0440\u043e\u043d\u043d\u044b\u0435 \u0438\u0442\u0435\u0440\u0430\u0442\u043e\u0440\u044b(\u0442\u0430\u043a\u0438\u043c \u043e\u0431\u0440\u0430\u0437\u043e\u043c \u043c\u043e\u0436\u043d\u043e \u0443\u0432\u044f\u0437\u0430\u0442\u044c \u0438\u0445 \u043c\u0435\u0436\u0434\u0443 \u0441\u043e\u0431\u043e\u0439). <\/p>\n<p>  \u0412 \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u0430\u0445 headers \u0438 cookies \u0442\u043e\u0436\u0435 \u043c\u043e\u0433\u0443\u0442 \u0431\u044b\u0442\u044c \u0444\u0443\u043d\u043a\u0446\u0438\u0438 \u0438 awaitable.<\/p>\n<p>  \u0410\u043f\u0438 \u043a\u0430\u0442\u0435\u0433\u043e\u0440\u0438\u0439 \u0432 \u043f\u0440\u0438\u043c\u0435\u0440\u0435 \u0432\u044b\u0448\u0435 \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u0442 \u043c\u0430\u0441\u0441\u0438\u0432 \u043e\u0431\u044a\u0435\u043a\u0442\u043e\u0432, \u0443 \u043a\u043e\u0442\u043e\u0440\u044b\u0445 \u0435\u0441\u0442\u044c slug, \u0438\u0442\u0435\u0440\u0430\u0442\u043e\u0440 \u0431\u0443\u0434\u0435\u0442 \u0432\u043e\u0437\u0432\u0440\u043e\u0430\u0449\u0430\u0442\u044c \u0438\u043c\u0435\u043d\u043d\u043e \u0438\u0445. \u041f\u043e\u0434\u0441\u0443\u043d\u0443\u0432 \u044d\u0442\u043e\u0442 \u0438\u0442\u0435\u0440\u0430\u0442\u043e\u0440 \u0432 url_params \u043f\u043e\u0441\u0442\u043e\u0432, \u0438\u0442\u0435\u0440\u0430\u0442\u043e\u0440 \u043f\u0440\u043e\u0439\u0434\u0435\u0442\u0441\u044f \u0440\u0435\u043a\u0443\u0440\u0441\u0438\u0432\u043d\u043e \u043f\u043e \u0432\u0441\u0435\u043c \u043a\u0430\u0442\u0435\u0433\u043e\u0440\u0438\u044f\u043c \u0438 \u043f\u043e \u0432\u0441\u0435\u043c \u0441\u0442\u0440\u0430\u043d\u0438\u0446\u0430\u043c \u0432 \u043a\u0430\u0436\u0434\u043e\u0439. \u041e\u043d \u043f\u0440\u0435\u0440\u0432\u0435\u0442\u0441\u044f \u043a\u043e\u0433\u0434\u0430 \u043d\u0430\u0442\u043a\u043d\u0435\u0442\u0441\u044f \u043d\u0430 404 \u0438\u043b\u0438 \u043a\u0430\u043a\u0443\u044e-\u0442\u043e \u0434\u0440\u0443\u0433\u0443\u044e \u043e\u0448\u0438\u0431\u043a\u0443 \u0438 \u043f\u0435\u0440\u0435\u0439\u0434\u0435\u0442 \u043a \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u0439 \u043a\u0430\u0442\u0435\u0433\u043e\u0440\u0438\u0438.<\/p>\n<p>  \u0410 \u0440\u0435\u043f\u043e\u0437\u0438\u0442\u043e\u0440\u0438\u0438 \u0435\u0441\u0442\u044c \u043f\u0440\u0438\u043c\u0435\u0440 aiohttp \u0441\u0435\u0440\u0432\u0435\u0440\u0430 \u0434\u043b\u044f \u044d\u0442\u0438\u0445 \u043a\u043b\u0430\u0441\u0441\u043e\u0432 \u0447\u0442\u043e\u0431\u044b \u0432\u0441\u0451 \u043c\u043e\u0436\u043d\u043e \u0431\u044b\u043b\u043e \u043f\u0440\u043e\u0442\u0435\u0441\u0442\u0438\u0440\u043e\u0432\u0430\u0442\u044c.<\/p>\n<p>  \u041f\u043e\u043c\u0438\u043c\u043e get \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u043e\u0432 \u043c\u043e\u0436\u043d\u043e \u043f\u0435\u0440\u0435\u0434\u0430\u0432\u0430\u0442\u044c \u0438\u0445 \u043a\u0430\u043a data \u0438\u043b\u0438 json \u0438 \u0437\u0430\u0434\u0430\u0442\u044c \u0434\u0440\u0443\u0433\u043e\u0439 method.<\/p>\n<p>  results_key \u0440\u0430\u0437\u0431\u0438\u0432\u0430\u0435\u0442\u0441\u044f \u043f\u043e \u0442\u043e\u0447\u043a\u0435 \u0438 \u0431\u0443\u0434\u0435\u0442 \u043f\u044b\u0442\u0430\u0442\u044c\u0441\u044f \u0432\u044b\u0434\u0435\u0440\u0433\u0438\u0432\u0430\u0442\u044c \u043a\u043b\u044e\u0447\u0438 \u0438\u0437 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u043e\u0432. \u041d\u0430\u043f\u0440\u0438\u043c\u0435\u0440 \u00abcomments.*.text\u00bb \u0432\u0435\u0440\u043d\u0435\u0442 \u0442\u0435\u043a\u0441\u0442 \u043a\u0430\u0436\u0434\u043e\u0433\u043e \u043a\u043e\u043c\u043c\u0435\u043d\u0442\u0430\u0440\u0438\u044f \u0438\u0437 \u043c\u0430\u0441\u0441\u0438\u0432\u0430 \u0432\u043d\u0443\u0442\u0440\u0438 comments.<\/p>\n<p>  \u0420\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u044b \u043e\u0431\u043e\u0440\u0430\u0447\u0438\u0432\u0430\u044e\u0442\u0441\u044f \u0432\u043e wrapper \u0443 \u043a\u043e\u0442\u043e\u0440\u043e\u0433\u043e \u0435\u0441\u0442\u044c \u0441\u0432\u043e\u0439\u0441\u0442\u0432\u0430 url \u0438 params. url \u044d\u0442\u043e \u043f\u0440\u043e\u0438\u0437\u0432\u043e\u0434\u043d\u043e\u0435 \u0441\u0442\u0440\u043e\u043a\u0438, \u0443 \u043a\u043e\u0442\u043e\u0440\u043e\u0439 \u0442\u043e\u0436\u0435 \u0435\u0441\u0442\u044c params. \u0422\u0430\u043a\u0438\u043c \u043e\u0431\u0440\u0430\u0437\u043e\u043c \u043c\u043e\u0436\u043d\u043e \u0443\u0437\u043d\u0430\u0442\u044c \u043a\u0430\u043a\u0438\u0435 \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u044b \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043b\u0438\u0441\u044c \u0434\u043b\u044f \u043f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u044f \u0434\u0430\u043d\u043d\u043e\u0433\u043e \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u0430 \u042d\u0442\u043e \u0434\u0435\u043c\u043e\u043d\u0441\u0442\u0440\u0438\u0440\u0443\u0435\u0442\u0441\u044f \u0432 \u043c\u0435\u0442\u043e\u0434\u0435 comments.<\/p>\n<p>  \u0422\u0430\u043a\u0436\u0435 \u0442\u0430\u043c \u0435\u0441\u0442\u044c \u0431\u0430\u0437\u043e\u0432\u044b\u0439 \u043a\u043b\u0430\u0441\u0441 Sink \u0434\u043b\u044f \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u043a\u0438 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u043e\u0432. \u041d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u0441\u043a\u043b\u0430\u0434\u044b\u0432\u0430\u043d\u0438\u044f \u0438\u0445 \u0432 mq \u0438\u043b\u0438 \u0431\u0430\u0437\u0443 \u0434\u0430\u043d\u043d\u044b\u0445. \u041e\u043d \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442 \u0432 \u043e\u0442\u0434\u0435\u043b\u044c\u043d\u044b\u0445 \u0442\u0430\u0441\u043a\u0430\u0445 \u0438 \u043f\u043e\u043b\u0443\u0447\u0430\u0435\u0442 \u0434\u0430\u043d\u043d\u044b\u0435 \u0447\u0435\u0440\u0435\u0437 asyncio.Queue.<\/p>\n<pre><code class=\"python\">class LoggingSink(Sink):     def transform(self, obj):         return repr(obj)      async def init(self):         from loguru import logger          self.logger = logger      async def process(self, obj):         self.logger.info(obj)         return True  sink = LoggingSink(num_tasks=1) <\/code><\/pre>\n<p>  \u041f\u0440\u0438\u043c\u0435\u0440 \u043f\u0440\u043e\u0441\u0442\u0435\u0439\u0448\u0435\u0433\u043e Sink. \u041c\u0435\u0442\u043e\u0434 transform \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u0435\u0442 \u043f\u0440\u043e\u0432\u0435\u0441\u0442\u0438 \u043a\u0430\u043a\u0438\u0435-\u0442\u043e \u043c\u0430\u043d\u0438\u043f\u0443\u043b\u044f\u0446\u0438\u0438 \u0441 \u043e\u0431\u044a\u0435\u043a\u0442\u043e\u043c \u0438 \u0432\u0435\u0440\u043d\u0443\u0442\u044c None, \u0435\u0441\u043b\u0438 \u043e\u043d \u043d\u0430\u043c \u043d\u0435 \u043f\u043e\u0434\u0445\u043e\u0434\u0438\u0442. \u0442.\u0435. \u0432 \u0442\u0435\u043c \u0442\u0430\u043a\u0436\u0435 \u043c\u043e\u0436\u043d\u043e \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u0432\u0430\u043b\u0438\u0434\u0430\u0446\u0438\u044e.<\/p>\n<p>  Sink \u044d\u0442\u043e \u0430\u0441\u0438\u043d\u0445\u0440\u043e\u043d\u043d\u044b\u0439 contextmanager, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043f\u0440\u0438 \u0432\u044b\u0445\u043e\u0434\u0435 \u043f\u043e-\u0438\u0434\u0435\u0435 \u0431\u0443\u0434\u0435\u0442 \u0436\u0434\u0430\u0442\u044c \u043f\u043e\u043a\u0430 \u0432\u0441\u0435 \u043e\u0431\u044a\u0435\u043a\u0442\u044b \u0432 \u043e\u0447\u0435\u0440\u0435\u0434\u0438 \u0431\u0443\u0434\u0443\u0442 \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u0430\u043d\u044b, \u043f\u043e\u0442\u043e\u043c \u043e\u0442\u043c\u0435\u043d\u0438\u0442 \u0441\u0432\u043e\u0438 \u0442\u0430\u0441\u043a\u0438.<\/p>\n<p>  \u041d\u0443 \u0438, \u043d\u0430\u043a\u043e\u043d\u0435\u0446, \u0434\u043b\u044f \u0441\u0432\u044f\u0437\u043a\u0438 \u044d\u0442\u043e\u0433\u043e \u0432\u0441\u0435\u0433\u043e \u0432\u043c\u0435\u0441\u0442\u0435 \u044f \u0441\u0434\u0435\u043b\u0430\u043b \u043a\u043b\u0430\u0441\u0441 Worker. \u041e\u043d \u043f\u0440\u0438\u043d\u0438\u043c\u0430\u0435\u0442 \u043e\u0434\u0438\u043d endpoint \u0438 \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u043e sink`\u043e\u0432. \u041d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, <\/p>\n<pre><code class=\"python\">worker = Worker(endpoint=posts, sinks=[loggingsink, mongosink]) worker.run() <\/code><\/pre>\n<p>  run \u0437\u0430\u043f\u0443\u0441\u0442\u0438\u0442 asyncio.run_until_complete \u0434\u043b\u044f pipeline`\u0430 worker`\u0430. \u0423 \u043d\u0435\u0433\u043e \u0442\u0430\u043a\u0436\u0435 \u0435\u0441\u0442\u044c \u043c\u0435\u0442\u043e\u0434 transform.<\/p>\n<p>  \u0415\u0449\u0451 \u0435\u0441\u0442\u044c \u043a\u043b\u0430\u0441\u0441 WorkerGroup \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u0435\u0442 \u0441\u043e\u0437\u0434\u0430\u0442\u044c \u0441\u0440\u0430\u0437\u0443 \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u043e \u0432\u043e\u0440\u043a\u0435\u0440\u043e\u0432 \u0438 \u0441\u0434\u0435\u043b\u0430\u0442\u044c asyncio.gather \u0434\u043b\u044f \u043d\u0438\u0445.<\/p>\n<p>  \u0412 \u043a\u043e\u0434\u0435 \u0435\u0441\u0442\u044c \u043f\u0440\u0438\u043c\u0435\u0440 \u0441\u0435\u0440\u0432\u0435\u0440\u0430, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0433\u0435\u043d\u0435\u0440\u0438\u0442 \u0434\u0430\u043d\u043d\u044b\u0435 \u0447\u0435\u0440\u0435\u0437 faker \u0438 \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u0447\u0438\u043a\u043e\u0432 \u0434\u043b\u044f \u0435\u0433\u043e endpoint`\u043e\u0432. \u0414\u0443\u043c\u0430\u044e \u044d\u0442\u043e \u043d\u0430\u0433\u043b\u044f\u0434\u043d\u0435\u0435 \u0432\u0441\u0435\u0433\u043e.<\/p>\n<p>  \u0412\u0441\u0451 \u044d\u0442\u043e \u043d\u0430 \u0440\u0430\u043d\u043d\u0435\u0439 \u0441\u0442\u0430\u0434\u0438\u0438 \u0440\u0430\u0437\u0432\u0438\u0442\u0438\u044f \u0438 \u044f \u043f\u043e\u043a\u0430 \u0447\u0442\u043e \u0447\u0430\u0441\u0442\u043e \u043c\u0435\u043d\u044f\u043b api. \u041d\u043e \u0441\u0435\u0439\u0447\u0430\u0441 \u0432\u0440\u043e\u0434\u0435 \u043f\u0440\u0438\u0448\u0435\u043b \u043a \u0442\u043e\u043c\u0443 \u043a\u0430\u043a \u044d\u0442\u043e \u0434\u043e\u043b\u0436\u043d\u043e \u0432\u044b\u0433\u043b\u044f\u0434\u0435\u0442\u044c. \u0411\u0443\u0434\u0443 \u0440\u0430\u0437 merge request`\u0430\u043c \u0438 \u043a\u043e\u043c\u043c\u0435\u043d\u0442\u0430\u0440\u0438\u044f\u043c \u043a \u043c\u043e\u0435\u043c\u0443 \u043a\u043e\u0434\u0443.<\/div>\n<p> \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\/525912\/\"> https:\/\/habr.com\/ru\/post\/525912\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"\n<div class=\"post__text post__text-html post__text_v1\" id=\"post-content-body\">\u0412\u0441\u0435\u043c \u043f\u0440\u0438\u043c\u0435\u0440. \u042f \u043d\u0430\u0447\u0430\u043b \u0440\u0430\u0431\u043e\u0442\u0430\u0442\u044c \u043d\u0430\u0434 <a href=\"https:\/\/github.com\/pawnhearts\/aio_api_crawler\" rel=\"nofollow\">\u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u043e\u0439<\/a> \u0434\u043b\u044f \u0432\u044b\u0434\u0435\u0440\u0433\u0438\u0432\u0430\u043d\u0438\u044f \u0434\u0430\u043d\u043d\u044b\u0445 \u0438\u0437 \u0440\u0430\u0437\u043d\u044b\u0445 json api. \u0422\u0430\u043a\u0436\u0435 \u043e\u043d\u0430 \u043c\u043e\u0436\u0435\u0442 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c\u0441\u044f \u0434\u043b\u044f \u0442\u0435\u0441\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f api.  <\/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-312344","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/312344","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=312344"}],"version-history":[{"count":0,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/312344\/revisions"}],"wp:attachment":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=312344"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=312344"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=312344"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}