{"id":375313,"date":"2024-05-21T06:21:28","date_gmt":"2024-05-21T06:21:28","guid":{"rendered":"http:\/\/savepearlharbor.com\/?p=375313"},"modified":"-0001-11-30T00:00:00","modified_gmt":"-0001-11-29T21:00:00","slug":"","status":"publish","type":"post","link":"https:\/\/savepearlharbor.com\/?p=375313","title":{"rendered":"<span>\u0412\u0435\u0431-\u0441\u043a\u0440\u0430\u043f\u0438\u043d\u0433 \u0441 Scrapy \u043d\u0430 Python<\/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>\u041f\u0440\u0438\u0432\u0435\u0442, \u0425\u0430\u0431\u0440! \u0421\u0435\u0433\u043e\u0434\u043d\u044f \u043c\u044b \u043f\u043e\u0433\u043e\u0432\u043e\u0440\u0438\u043c \u043e \u0445\u043e\u0440\u043e\u0448\u0435\u043c \u0438\u043d\u0441\u0442\u0440\u0443\u043c\u0435\u043d\u0442\u0435 \u0434\u043b\u044f \u0432\u0435\u0431-\u0441\u043a\u0440\u0430\u043f\u0438\u043d\u0433\u0430, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0437\u0430\u0440\u0435\u043a\u043e\u043c\u0435\u043d\u0434\u043e\u0432\u0430\u043b \u0441\u0435\u0431\u044f, \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u0435 Scrapy \u0434\u043b\u044f Python. <\/p>\n<p>\u0423\u0441\u0442\u0430\u043d\u043e\u0432\u0438\u043c:<\/p>\n<pre><code class=\"bash\">pip install Scrapy<\/code><\/pre>\n<h4>\u041d\u0430\u0447\u043d\u0435\u043c<\/h4>\n<p>Scrapy \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442 \u0441\u043f\u0435\u0446. \u043a\u043b\u0430\u0441\u0441\u044b, \u043d\u0430\u0437\u044b\u0432\u0430\u0435\u043c\u044b\u0435 <em>\u043f\u0430\u0443\u043a\u0430\u043c\u0438<\/em>, \u0432 \u043a\u043e\u0434\u0435 &#8212; spiders, \u0434\u043b\u044f \u0441\u0431\u043e\u0440\u0430 \u0438\u043d\u0444\u043e\u0440\u043c\u0430\u0446\u0438\u0438 \u0441 \u0432\u0435\u0431-\u0441\u0442\u0440\u0430\u043d\u0438\u0446:<\/p>\n<pre><code class=\"python\">import scrapy  class ExampleSpider(scrapy.Spider):     name = 'otus'     allowed_domains = ['example.com']     start_urls = ['http:\/\/example.com\/']      def parse(self, response):         title = response.css('title::text').get()         yield {'title': title}<\/code><\/pre>\n<ul>\n<li>\n<p><code>name<\/code> \u2014 \u0438\u043c\u044f \u043f\u0430\u0443\u0447\u043a\u0430.<\/p>\n<\/li>\n<li>\n<p><code>allowed_domains<\/code> \u2014 \u0441\u043f\u0438\u0441\u043e\u043a \u0434\u043e\u043c\u0435\u043d\u043e\u0432, \u0441 \u043a\u043e\u0442\u043e\u0440\u044b\u0445 \u043f\u0430\u0443\u043a \u043c\u043e\u0436\u0435\u0442 \u0441\u043e\u0431\u0438\u0440\u0430\u0442\u044c \u0434\u0430\u043d\u043d\u044b\u0435.<\/p>\n<\/li>\n<li>\n<p><code>start_urls<\/code> \u2014 \u0441\u043f\u0438\u0441\u043e\u043a URL-\u0430\u0434\u0440\u0435\u0441\u043e\u0432, \u0441 \u043a\u043e\u0442\u043e\u0440\u044b\u0445 \u043f\u0430\u0443\u043a \u043d\u0430\u0447\u0438\u043d\u0430\u0435\u0442 \u0441\u0431\u043e\u0440 \u0434\u0430\u043d\u043d\u044b\u0445.<\/p>\n<\/li>\n<li>\n<p> <code>parse<\/code> \u0432\u044b\u0437\u044b\u0432\u0430\u0435\u0442\u0441\u044f \u0434\u043b\u044f \u043a\u0430\u0436\u0434\u043e\u0433\u043e \u043e\u0442\u0432\u0435\u0442\u0430, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043f\u0430\u0443\u043a \u043f\u043e\u043b\u0443\u0447\u0430\u0435\u0442 \u0432 \u043e\u0442\u0432\u0435\u0442 \u043d\u0430 \u0437\u0430\u043f\u0440\u043e\u0441\u044b URL \u0438\u0437 <code>start_urls<\/code>.<\/p>\n<\/li>\n<\/ul>\n<p>\u0417\u0430\u043f\u0443\u0441\u043a \u043f\u0430\u0443\u043a\u0430:<\/p>\n<pre><code class=\"bash\">scrapy crawl myspider<\/code><\/pre>\n<p>\u0414\u043b\u044f \u0440\u0430\u0431\u043e\u0442\u044b \u0441 <strong>\u043f\u0430\u0433\u0438\u043d\u0430\u0446\u0438\u0435\u0439<\/strong> \u043c\u043e\u0436\u043d\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u043e \u043f\u043e\u0434\u0445\u043e\u0434\u043e\u0432. \u041e\u0434\u0438\u043d \u0438\u0437 \u043d\u0438\u0445 &#8212; \u044d\u0442\u043e \u043f\u0440\u044f\u043c\u043e\u0435 \u0441\u043b\u0435\u0434\u043e\u0432\u0430\u043d\u0438\u0435 \u043f\u043e \u0441\u0441\u044b\u043b\u043a\u0430\u043c \u043d\u0430 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0435 \u0441\u0442\u0440\u0430\u043d\u0438\u0446\u044b. \u041c\u043e\u0436\u043d\u043e \u0440\u0435\u0430\u043b\u0438\u0437\u043e\u0432\u0430\u0442\u044c, \u0438\u0437\u0432\u043b\u0435\u043a\u0430\u044f URL \u0441\u043b\u0435\u0434. \u0441\u0442\u0440\u0430\u043d\u0438\u0446\u044b \u0438 \u043f\u0435\u0440\u0435\u0434\u0430\u0432\u0430\u044f \u0435\u0433\u043e \u0432 \u043c\u0435\u0442\u043e\u0434 <code>follow<\/code> Scrapy. \u041d\u0430\u043f\u0440\u0438\u043c\u0435\u0440:<\/p>\n<pre><code class=\"python\">def parse(self, response):     # \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u043a\u0430 \u0434\u0430\u043d\u043d\u044b\u0445 \u0441\u0442\u0440\u0430\u043d\u0438\u0446\u044b     ...     next_page = response.css(\"li.next a::attr(href)\").get()     if next_page is not None:         yield response.follow(next_page, self.parse)<\/code><\/pre>\n<p>\u0414\u043b\u044f \u0430\u0432\u0442\u043e\u043c\u0430\u0442\u0438\u0437\u0430\u0446\u0438\u0438 \u0432\u0445\u043e\u0434\u0430 \u043d\u0430 \u0441\u0430\u0439\u0442\u044b \u043c\u043e\u0436\u043d\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0444\u043e\u0440\u043c\u044b \u0432\u0445\u043e\u0434\u0430, \u0430\u0432\u0442\u043e\u043c\u0430\u0442\u043e\u043c \u0437\u0430\u043f\u043e\u043b\u043d\u044f\u044f \u0438 \u043e\u0442\u043f\u0440\u0430\u0432\u043b\u044f\u044f \u0434\u0430\u043d\u043d\u044b\u0435 \u0444\u043e\u0440\u043c. Scrapy \u043f\u043e\u0434\u0434\u0435\u0440\u0436\u0438\u0432\u0430\u0435\u0442 \u043e\u0442\u043f\u0440\u0430\u0432\u043a\u0443 \u0444\u043e\u0440\u043c \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u043a\u043b\u0430\u0441\u0441\u0430 <code>FormRequest<\/code>:<\/p>\n<pre><code class=\"python\">from scrapy.http import FormRequest  def start_requests(self):     return [FormRequest(\"http:\/\/example.com\/login\", formdata={'user': 'john', 'pass': 'secret'}, callback=self.logged_in)]  def logged_in(self, response):     # \u043f\u0440\u043e\u0434\u043e\u043b\u0436\u0435\u043d\u0438\u0435 \u0441\u043a\u0440\u0430\u043f\u0438\u043d\u0433\u0430 \u043f\u043e\u0441\u043b\u0435 \u0432\u0445\u043e\u0434\u0430     pass<\/code><\/pre>\n<p>\u0414\u043b\u044f \u0441\u0442\u0440\u0430\u043d\u0438\u0446, \u0433\u0434\u0435 \u043a\u043e\u043d\u0442\u0435\u043d\u0442 \u0437\u0430\u0433\u0440\u0443\u0436\u0430\u0435\u0442\u0441\u044f \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e JS, \u043c\u043e\u0436\u043d\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c <a href=\"https:\/\/splash.readthedocs.io\/en\/stable\/\">Splash <\/a>\u0438\u043b\u0438 <a href=\"https:\/\/selenium-python.readthedocs.io\/\">Selenium<\/a>, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u044e\u0442 \u0438\u0441\u043f\u043e\u043b\u043d\u044f\u0442\u044c JS \u043f\u0435\u0440\u0435\u0434 \u0438\u0437\u0432\u043b\u0435\u0447\u0435\u043d\u0438\u0435\u043c \u0434\u0430\u043d\u043d\u044b\u0445. \u041d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u0442\u043e\u0442 \u0436\u0435 Splash \u0438\u043d\u0442\u0435\u0433\u0440\u0438\u0440\u0443\u0435\u0442\u0441\u044f \u0441 Scrapy \u0447\u0435\u0440\u0435\u0437 scrapy-splash, \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u044f \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0442\u044c JavaScript \u0438 \u043f\u043e\u043b\u0443\u0447\u0430\u0442\u044c \u0430\u043a\u0442\u0443\u0430\u043b\u044c\u043d\u044b\u0439 HTML. \u041f\u0440\u0438\u043c\u0435\u0440:<\/p>\n<pre><code class=\"python\">from scrapy_splash import SplashRequest  def start_requests(self):     yield SplashRequest(url='http:\/\/example.com', callback=self.parse, endpoint='render.html', args={'wait': 0.5})  def parse(self, response):     # \u0438\u0437\u0432\u043b\u0435\u0447\u0435\u043d\u0438\u0435 \u0434\u0430\u043d\u043d\u044b\u0445     pass<\/code><\/pre>\n<p>Scrapy \u043f\u043e\u0434\u0434\u0435\u0440\u0436\u0438\u0432\u0430\u0435\u0442 \u044d\u043a\u0441\u043f\u043e\u0440\u0442 \u0434\u0430\u043d\u043d\u044b\u0445 \u0432 \u0444\u043e\u0440\u043c\u0430\u0442\u044b <em>JSON<\/em>, <em>CSV<\/em> \u0438 <em>XML<\/em> \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u0432\u0441\u0442\u0440\u043e\u0435\u043d\u043d\u044b\u0445 \u044d\u043a\u0441\u043f\u043e\u0440\u0442\u0435\u0440\u043e\u0432. \u042d\u043a\u0441\u043f\u043e\u0440\u0442 \u0434\u0430\u043d\u043d\u044b\u0445 \u043c\u043e\u0436\u0435\u0442 \u0431\u044b\u0442\u044c \u043d\u0430\u0441\u0442\u0440\u043e\u0435\u043d \u0447\u0435\u0440\u0435\u0437 <code>settings.py<\/code> \u043f\u0440\u043e\u0435\u043a\u0442\u0430 \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u0430 <code>FEEDS<\/code>, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u0435\u0442 \u0443\u043a\u0430\u0437\u0430\u0442\u044c \u0440\u0430\u0437\u043b\u0438\u0447\u043d\u044b\u0435 \u043d\u0430\u0441\u0442\u0440\u043e\u0439\u043a\u0438 \u044d\u043a\u0441\u043f\u043e\u0440\u0442\u0430 \u0434\u043b\u044f \u043a\u0430\u0436\u0434\u043e\u0433\u043e \u0442\u0438\u043f\u0430 \u0444\u0430\u0439\u043b\u0430:<\/p>\n<pre><code class=\"python\"># \u043d\u0430\u0441\u0442\u0440\u043e\u0439\u043a\u0430 \u044d\u043a\u0441\u043f\u043e\u0440\u0442\u0430 \u0434\u0430\u043d\u043d\u044b\u0445 \u0432 JSON \u0438 CSV FEEDS = {     'items.json': {         'format': 'json',         'encoding': 'utf8',         'store_empty': False,         'fields': None,         'indent': 4,     },     'items.csv': {         'format': 'csv',         'fields': ['field1', 'field2', 'field3'],  # \u0441\u043f\u0435\u0446\u0438\u0444\u0438\u043a\u0430\u0446\u0438\u044f \u043f\u043e\u043b\u0435\u0439 \u0434\u043b\u044f \u044d\u043a\u0441\u043f\u043e\u0440\u0442\u0430     } }<\/code><\/pre>\n<p><strong>Item Pipelines<\/strong> \u0432 Scrapy \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u044e\u0442 \u043f\u0440\u043e\u0432\u043e\u0434\u0438\u0442\u044c \u043f\u043e\u0441\u043b\u0435\u043f\u0440\u043e\u0446\u0435\u0441\u0441\u0438\u043d\u0433 \u0441\u043e\u0431\u0440\u0430\u043d\u043d\u044b\u0445 \u0434\u0430\u043d\u043d\u044b\u0445, \u0432\u043a\u043b\u044e\u0447\u0430\u044f \u0438\u0445 \u0432\u0430\u043b\u0438\u0434\u0430\u0446\u0438\u044e, \u043e\u0447\u0438\u0441\u0442\u043a\u0443 \u0438 \u0441\u043e\u0445\u0440\u0430\u043d\u0435\u043d\u0438\u0435 \u0432 \u0411\u0414. \u041d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u0434\u043b\u044f \u0441\u043e\u0445\u0440\u0430\u043d\u0435\u043d\u0438\u044f \u0434\u0430\u043d\u043d\u044b\u0445 \u0432 \u0411\u0414 MySQL \u0438\u043b\u0438 PostgreSQL, \u043c\u043e\u0436\u043d\u043e \u043d\u0430\u0441\u0442\u0440\u043e\u0438\u0442\u044c pipeline, \u043a\u043e\u0442\u043e\u0440\u0430\u044f \u0431\u0443\u0434\u0435\u0442 \u043e\u0431\u0440\u0430\u0431\u0430\u0442\u044b\u0432\u0430\u0442\u044c \u043a\u0430\u0436\u0434\u044b\u0439 \u0441\u043e\u0431\u0440\u0430\u043d\u043d\u044b\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442:<\/p>\n<pre><code class=\"python\">import psycopg2  class PostgresPipeline(object):     def open_spider(self, spider):         hostname = 'localhost'         username = 'postgres'         password = 'yourpassword'         database = 'scrapy_db'         self.connection = psycopg2.connect(host=hostname, user=username, password=password, dbname=database)         self.cur = self.connection.cursor()      def process_item(self, item, spider):         self.cur.execute(\"INSERT INTO table_name (field1, field2) VALUES (%s, %s)\",                           (item['field1'], item['field2']))         self.connection.commit()         return item      def close_spider(self, spider):         self.cur.close()         self.connection.close()<\/code><\/pre>\n<p>\u042d\u0442\u043e\u0442 pipeline \u0443\u0441\u0442\u0430\u043d\u0430\u0432\u043b\u0438\u0432\u0430\u0435\u0442 \u0441\u043e\u0435\u0434\u0438\u043d\u0435\u043d\u0438\u0435 \u0441 \u0411\u0414 \u043f\u0440\u0438 \u043e\u0442\u043a\u0440\u044b\u0442\u0438\u0438 \u043f\u0430\u0443\u043a\u0430, \u043e\u0431\u0440\u0430\u0431\u0430\u0442\u044b\u0432\u0430\u0435\u0442 \u043a\u0430\u0436\u0434\u044b\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u043f\u0440\u0438 \u0435\u0433\u043e \u0441\u0431\u043e\u0440\u0435, \u0432\u0441\u0442\u0430\u0432\u043b\u044f\u044f \u0434\u0430\u043d\u043d\u044b\u0435 \u0432 \u0442\u0430\u0431\u043b\u0438\u0446\u0443, \u0438 \u0437\u0430\u043a\u0440\u044b\u0432\u0430\u0435\u0442 \u0441\u043e\u0435\u0434\u0438\u043d\u0435\u043d\u0438\u0435 \u043f\u043e\u0441\u043b\u0435 \u0437\u0430\u0432\u0435\u0440\u0448\u0435\u043d\u0438\u044f \u0440\u0430\u0431\u043e\u0442\u044b \u043f\u0430\u0443\u043a\u0430.<\/p>\n<h4>\u041f\u0440\u0438\u043c\u0435\u0440 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u044f<\/h4>\n<p>\u0421\u043e\u0437\u0434\u0430\u0434\u0438\u043c \u043f\u0430\u0440\u0441\u0435\u0440, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0431\u0443\u0434\u0435\u0442 \u0431\u0443\u0434\u0435\u0442 \u0441\u043e\u0431\u0438\u0440\u0430\u0442\u044c \u0434\u0430\u043d\u043d\u044b\u0435 \u0441 \u0432\u044b\u043c\u044b\u0448\u043b\u0435\u043d\u043d\u043e\u0433\u043e \u0441\u0430\u0439\u0442\u0430 \u043a\u043d\u0438\u0436\u043d\u043e\u0433\u043e \u043c\u0430\u0433\u0430\u0437\u0438\u043d\u0430, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0438\u043c\u0435\u0435\u0442 \u043f\u0430\u0433\u0438\u043d\u0430\u0446\u0438\u044e, \u0442\u0440\u0435\u0431\u0443\u0435\u0442 \u0430\u0432\u0442\u043e\u0440\u0438\u0437\u0430\u0446\u0438\u0438 \u0434\u043b\u044f \u0434\u043e\u0441\u0442\u0443\u043f\u0430 \u043a \u043d\u0435\u043a\u043e\u0442\u043e\u0440\u044b\u043c \u0441\u0442\u0440\u0430\u043d\u0438\u0446\u0430\u043c \u0438 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442 JS \u0434\u043b\u044f \u043e\u0442\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u044f \u043d\u0435\u043a\u043e\u0442\u043e\u0440\u044b\u0445 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432.<\/p>\n<p>\u0421\u043e\u0437\u0434\u0430\u0434\u0438\u043c \u043d\u043e\u0432\u044b\u0439 \u043f\u0440\u043e\u0435\u043a\u0442 Scrapy:<\/p>\n<pre><code class=\"bash\">scrapy startproject books_scraper<\/code><\/pre>\n<p>\u041f\u0435\u0440\u0435\u0445\u043e\u0434\u0438\u043c \u0432 \u0434\u0438\u0440\u0435\u043a\u0442\u043e\u0440\u0438\u044e \u043f\u0440\u043e\u0435\u043a\u0442\u0430:<\/p>\n<pre><code class=\"bash\">cd books_scraper<\/code><\/pre>\n<p>\u0412 \u0434\u0438\u0440\u0435\u043a\u0442\u043e\u0440\u0438\u0438 <code>spiders<\/code> \u0441\u043e\u0437\u0434\u0430\u0434\u0438\u043c \u043d\u043e\u0432\u043e\u0433\u043e \u043f\u0430\u0443\u043a\u0430 <code>book_spider.py<\/code>:<\/p>\n<pre><code class=\"python\">import scrapy from scrapy_splash import SplashRequest from scrapy.http import FormRequest from books_scraper.items import BookItem  # \u043d\u0443\u0436\u0435\u043d \u043a\u043b\u0430\u0441\u0441 Item \u0432 items.py  class BookSpider(scrapy.Spider):     name = 'book_spider'     login_url = 'http:\/\/books.example.com\/login'     start_urls = ['http:\/\/books.example.com']      def start_requests(self):         #\u043f\u0435\u0440\u0435\u0445\u043e\u0434 \u043d\u0430 \u0441\u0442\u0440\u0430\u043d\u0438\u0446\u0443 \u0432\u0445\u043e\u0434\u0430         yield scrapy.Request(self.login_url, self.parse_login)      def parse_login(self, response):         # \u043e\u0442\u043f\u0440\u0430\u0432\u043a\u0430 \u0434\u0430\u043d\u043d\u044b\u0445 \u0434\u043b\u044f \u0432\u0445\u043e\u0434\u0430         yield FormRequest.from_response(response,                                         formdata={'username': 'user', 'password': 'pass'},                                         callback=self.after_login)      def after_login(self, response):         # \u043f\u0440\u043e\u0432\u0435\u0440\u043a\u0430 \u0443\u0441\u043f\u0435\u0448\u043d\u043e\u0433\u043e \u0432\u0445\u043e\u0434\u0430 \u0438 \u043f\u0440\u043e\u0434\u043e\u043b\u0436\u0435\u043d\u0438\u0435 \u0441\u043a\u0440\u0430\u043f\u0438\u043d\u0433\u0430         if \"authentication failed\" in response.body:             self.logger.error(\"Login failed\")             return         else:             # \u0437\u0430\u043f\u0443\u0441\u043a \u0441\u043a\u0440\u0430\u043f\u0438\u043d\u0433\u0430 \u0441 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0435\u043c Splash \u0434\u043b\u044f \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u043a\u0438 JavaScript             for url in self.start_urls:                 yield SplashRequest(url, self.parse, endpoint='execute', args={'lua_source': self.lua_script()})      def parse(self, response):         # \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u043a\u0430 \u0434\u0430\u043d\u043d\u044b\u0445 \u0441\u0442\u0440\u0430\u043d\u0438\u0446\u044b         books = response.css('div.book')         for book in books:             item = BookItem()             item['title'] = book.css('h3.title::text').get()             item['price'] = book.css('p.price::text').get()             yield item          # \u043f\u0430\u0433\u0438\u043d\u0430\u0446\u0438\u044f         next_page = response.css('li.next a::attr(href)').get()         if next_page:             yield SplashRequest(next_page, self.parse, endpoint='execute', args={'lua_source': self.lua_script()})      def lua_script(self):         # Lua \u0441\u043a\u0440\u0438\u043f\u0442 \u0434\u043b\u044f Splash, \u0441\u043a\u0440\u043e\u043b\u043b\u0438\u043d\u0433 \u0441\u0442\u0440\u0430\u043d\u0438\u0446\u044b \u0438\u043b\u0438 \u0434\u0440\u0443\u0433\u0438\u0435 \u0434\u0435\u0439\u0441\u0442\u0432\u0438\u044f         return \"\"\"         function main(splash)             splash:set_user_agent(splash.args.ua)             assert(splash:go(splash.args.url))             assert(splash:wait(1))             return {html = splash:html()}         end         \"\"\"<\/code><\/pre>\n<p>\u0412 \u0444\u0430\u0439\u043b\u0435 <code>items.py<\/code> \u0441\u043e\u0437\u0434\u0430\u0434\u0438\u043c \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u0443 \u0434\u0430\u043d\u043d\u044b\u0445 \u0434\u043b\u044f \u043a\u043d\u0438\u0433:<\/p>\n<pre><code class=\"python\">import scrapy  class BookItem(scrapy.Item):     title = scrapy.Field()     price = scrapy.Field()<\/code><\/pre>\n<p>\u0412 <code>pipelines.py<\/code> \u0434\u043e\u0431\u0430\u0432\u0438\u043c \u043a\u043e\u0434 \u0434\u043b\u044f \u0441\u043e\u0445\u0440\u0430\u043d\u0435\u043d\u0438\u044f \u0434\u0430\u043d\u043d\u044b\u0445 \u0432 PostgreSQL:<\/p>\n<pre><code class=\"python\">import psycopg2  class PostgresPipeline(object):     def open_spider(self, spider):         self.connection = psycopg2.connect(host='localhost', dbname='scrapy_books', user='postgres', password='password')         self.cur = self.connection.cursor()      def close_spider(self, spider):         self.connection.close()      def process_item(self, item, spider):         self.cur.execute(\"INSERT INTO books (title, price) VALUES (%s, %s)\", (item['title'], item['price']))         self.connection.commit()         return item<\/code><\/pre>\n<p>\u0410\u043a\u0442\u0438\u0432\u0438\u0440\u0443\u0435\u043c <code>PostgresPipeline<\/code> \u0432 <code>settings.py<\/code> \u0438 \u043d\u0430\u0441\u0442\u0440\u0430\u0438\u0432\u0435\u043c Splash \u0432 \u0441\u043b\u0443\u0447\u0430\u0435 \u0440\u0430\u0431\u043e\u0442\u044b \u0441 JS:<\/p>\n<pre><code class=\"python\">ITEM_PIPELINES = {    'books_scraper.pipelines.PostgresPipeline': 300, }  SPLASH_URL = 'http:\/\/localhost:8050'<\/code><\/pre>\n<p>\u0417\u0430\u043f\u0443\u0441\u043a\u0430\u0435\u043c:<\/p>\n<pre><code class=\"bash\">scrapy crawl book_spider<\/code><\/pre>\n<hr\/>\n<p>\u041f\u043e\u0434\u0440\u043e\u0431\u043d\u0435\u0439 \u0441 Scrapy \u043c\u043e\u0436\u043d\u043e <a href=\"https:\/\/docs.scrapy.org\/en\/latest\/\">\u043e\u0437\u043d\u0430\u043a\u043e\u043c\u0438\u0442\u044c\u0441\u044f \u0432 \u0434\u043e\u043a\u0443\u043c\u0435\u043d\u0442\u0430\u0446\u0438\u0438.<\/a><\/p>\n<p>\u0412 \u0437\u0430\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u0435 \u0440\u0430\u0441\u0441\u043a\u0430\u0436\u0443 \u043e\u0431 \u043e\u0442\u043a\u0440\u044b\u0442\u044b\u0445 \u0443\u0440\u043e\u043a\u0430\u0445:<\/p>\n<ul>\n<li>\n<p>13 \u043c\u0430\u044f: \u0413\u0435\u043d\u0435\u0440\u0430\u0442\u043e\u0440\u044b \u0438 \u043e\u0447\u0435\u0440\u0435\u0434\u0438 \u0432 Python: \u0440\u0430\u0441\u0441\u043c\u043e\u0442\u0440\u0438\u043c \u043a\u043e\u043d\u0446\u0435\u043f\u0446\u0438\u0438 \u0433\u0435\u043d\u0435\u0440\u0430\u0442\u043e\u0440\u043d\u044b\u0445 \u0444\u0443\u043d\u043a\u0446\u0438\u0439 \u0438 \u043e\u0447\u0435\u0440\u0435\u0434\u0435\u0439, \u043f\u043e\u0433\u043e\u0432\u043e\u0440\u0438\u043c \u043e \u0441\u0445\u043e\u0434\u0441\u0442\u0432\u0430\u0445, \u0440\u0430\u0437\u043b\u0438\u0447\u0438\u044f\u0445, \u0438 \u043f\u0440\u0438\u043c\u0435\u043d\u0438\u043c\u043e\u0441\u0442\u0438 \u0432 \u0440\u0435\u0448\u0435\u043d\u0438\u0438 \u0440\u0435\u0430\u043b\u044c\u043d\u044b\u0445 \u0437\u0430\u0434\u0430\u0447. <a href=\"https:\/\/otus.pw\/Yhze\/\">\u0417\u0430\u043f\u0438\u0441\u0430\u0442\u044c\u0441\u044f<\/a><\/p>\n<\/li>\n<li>\n<p>28 \u043c\u0430\u044f: \u0421\u0430\u043c\u044b\u0435 \u044d\u0444\u0444\u0435\u043a\u0442\u0438\u0432\u043d\u044b\u0435 \u043e\u0434\u043d\u043e\u0441\u0442\u0440\u043e\u0447\u043d\u0438\u043a\u0438 \u0432 Python: \u043f\u043e\u0434\u0435\u043b\u0438\u043c\u0441\u044f \u0441\u0435\u043a\u0440\u0435\u0442\u0430\u043c\u0438 \u0438 \u043c\u0430\u0441\u0442\u0435\u0440\u0441\u0442\u0432\u043e\u043c \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u044f \u043c\u043e\u0449\u043d\u044b\u0445 \u043e\u0434\u043d\u043e\u0441\u0442\u0440\u043e\u0447\u043d\u0438\u043a\u043e\u0432 \u0432 Python. <a href=\"https:\/\/otus.pw\/Fqy2\/\">\u0417\u0430\u043f\u0438\u0441\u0430\u0442\u044c\u0441\u044f<\/a><\/p>\n<\/li>\n<\/ul>\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\/articles\/812035\/\"> https:\/\/habr.com\/ru\/articles\/812035\/<\/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>\u041f\u0440\u0438\u0432\u0435\u0442, \u0425\u0430\u0431\u0440! \u0421\u0435\u0433\u043e\u0434\u043d\u044f \u043c\u044b \u043f\u043e\u0433\u043e\u0432\u043e\u0440\u0438\u043c \u043e \u0445\u043e\u0440\u043e\u0448\u0435\u043c \u0438\u043d\u0441\u0442\u0440\u0443\u043c\u0435\u043d\u0442\u0435 \u0434\u043b\u044f \u0432\u0435\u0431-\u0441\u043a\u0440\u0430\u043f\u0438\u043d\u0433\u0430, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0437\u0430\u0440\u0435\u043a\u043e\u043c\u0435\u043d\u0434\u043e\u0432\u0430\u043b \u0441\u0435\u0431\u044f, \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u0435 Scrapy \u0434\u043b\u044f Python. <\/p>\n<p>\u0423\u0441\u0442\u0430\u043d\u043e\u0432\u0438\u043c:<\/p>\n<pre><code class=\"bash\">pip install Scrapy<\/code><\/pre>\n<h4>\u041d\u0430\u0447\u043d\u0435\u043c<\/h4>\n<p>Scrapy \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442 \u0441\u043f\u0435\u0446. \u043a\u043b\u0430\u0441\u0441\u044b, \u043d\u0430\u0437\u044b\u0432\u0430\u0435\u043c\u044b\u0435 <em>\u043f\u0430\u0443\u043a\u0430\u043c\u0438<\/em>, \u0432 \u043a\u043e\u0434\u0435 &#8212; spiders, \u0434\u043b\u044f \u0441\u0431\u043e\u0440\u0430 \u0438\u043d\u0444\u043e\u0440\u043c\u0430\u0446\u0438\u0438 \u0441 \u0432\u0435\u0431-\u0441\u0442\u0440\u0430\u043d\u0438\u0446:<\/p>\n<pre><code class=\"python\">import scrapy  class ExampleSpider(scrapy.Spider):     name = 'otus'     allowed_domains = ['example.com']     start_urls = ['http:\/\/example.com\/']      def parse(self, response):         title = response.css('title::text').get()         yield {'title': title}<\/code><\/pre>\n<ul>\n<li>\n<p><code>name<\/code> \u2014 \u0438\u043c\u044f \u043f\u0430\u0443\u0447\u043a\u0430.<\/p>\n<\/li>\n<li>\n<p><code>allowed_domains<\/code> \u2014 \u0441\u043f\u0438\u0441\u043e\u043a \u0434\u043e\u043c\u0435\u043d\u043e\u0432, \u0441 \u043a\u043e\u0442\u043e\u0440\u044b\u0445 \u043f\u0430\u0443\u043a \u043c\u043e\u0436\u0435\u0442 \u0441\u043e\u0431\u0438\u0440\u0430\u0442\u044c \u0434\u0430\u043d\u043d\u044b\u0435.<\/p>\n<\/li>\n<li>\n<p><code>start_urls<\/code> \u2014 \u0441\u043f\u0438\u0441\u043e\u043a URL-\u0430\u0434\u0440\u0435\u0441\u043e\u0432, \u0441 \u043a\u043e\u0442\u043e\u0440\u044b\u0445 \u043f\u0430\u0443\u043a \u043d\u0430\u0447\u0438\u043d\u0430\u0435\u0442 \u0441\u0431\u043e\u0440 \u0434\u0430\u043d\u043d\u044b\u0445.<\/p>\n<\/li>\n<li>\n<p> <code>parse<\/code> \u0432\u044b\u0437\u044b\u0432\u0430\u0435\u0442\u0441\u044f \u0434\u043b\u044f \u043a\u0430\u0436\u0434\u043e\u0433\u043e \u043e\u0442\u0432\u0435\u0442\u0430, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043f\u0430\u0443\u043a \u043f\u043e\u043b\u0443\u0447\u0430\u0435\u0442 \u0432 \u043e\u0442\u0432\u0435\u0442 \u043d\u0430 \u0437\u0430\u043f\u0440\u043e\u0441\u044b URL \u0438\u0437 <code>start_urls<\/code>.<\/p>\n<\/li>\n<\/ul>\n<p>\u0417\u0430\u043f\u0443\u0441\u043a \u043f\u0430\u0443\u043a\u0430:<\/p>\n<pre><code class=\"bash\">scrapy crawl myspider<\/code><\/pre>\n<p>\u0414\u043b\u044f \u0440\u0430\u0431\u043e\u0442\u044b \u0441 <strong>\u043f\u0430\u0433\u0438\u043d\u0430\u0446\u0438\u0435\u0439<\/strong> \u043c\u043e\u0436\u043d\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u043e \u043f\u043e\u0434\u0445\u043e\u0434\u043e\u0432. \u041e\u0434\u0438\u043d \u0438\u0437 \u043d\u0438\u0445 &#8212; \u044d\u0442\u043e \u043f\u0440\u044f\u043c\u043e\u0435 \u0441\u043b\u0435\u0434\u043e\u0432\u0430\u043d\u0438\u0435 \u043f\u043e \u0441\u0441\u044b\u043b\u043a\u0430\u043c \u043d\u0430 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0435 \u0441\u0442\u0440\u0430\u043d\u0438\u0446\u044b. \u041c\u043e\u0436\u043d\u043e \u0440\u0435\u0430\u043b\u0438\u0437\u043e\u0432\u0430\u0442\u044c, \u0438\u0437\u0432\u043b\u0435\u043a\u0430\u044f URL \u0441\u043b\u0435\u0434. \u0441\u0442\u0440\u0430\u043d\u0438\u0446\u044b \u0438 \u043f\u0435\u0440\u0435\u0434\u0430\u0432\u0430\u044f \u0435\u0433\u043e \u0432 \u043c\u0435\u0442\u043e\u0434 <code>follow<\/code> Scrapy. \u041d\u0430\u043f\u0440\u0438\u043c\u0435\u0440:<\/p>\n<pre><code class=\"python\">def parse(self, response):     # \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u043a\u0430 \u0434\u0430\u043d\u043d\u044b\u0445 \u0441\u0442\u0440\u0430\u043d\u0438\u0446\u044b     ...     next_page = response.css(\"li.next a::attr(href)\").get()     if next_page is not None:         yield response.follow(next_page, self.parse)<\/code><\/pre>\n<p>\u0414\u043b\u044f \u0430\u0432\u0442\u043e\u043c\u0430\u0442\u0438\u0437\u0430\u0446\u0438\u0438 \u0432\u0445\u043e\u0434\u0430 \u043d\u0430 \u0441\u0430\u0439\u0442\u044b \u043c\u043e\u0436\u043d\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0444\u043e\u0440\u043c\u044b \u0432\u0445\u043e\u0434\u0430, \u0430\u0432\u0442\u043e\u043c\u0430\u0442\u043e\u043c \u0437\u0430\u043f\u043e\u043b\u043d\u044f\u044f \u0438 \u043e\u0442\u043f\u0440\u0430\u0432\u043b\u044f\u044f \u0434\u0430\u043d\u043d\u044b\u0435 \u0444\u043e\u0440\u043c. Scrapy \u043f\u043e\u0434\u0434\u0435\u0440\u0436\u0438\u0432\u0430\u0435\u0442 \u043e\u0442\u043f\u0440\u0430\u0432\u043a\u0443 \u0444\u043e\u0440\u043c \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u043a\u043b\u0430\u0441\u0441\u0430 <code>FormRequest<\/code>:<\/p>\n<pre><code class=\"python\">from scrapy.http import FormRequest  def start_requests(self):     return [FormRequest(\"http:\/\/example.com\/login\", formdata={'user': 'john', 'pass': 'secret'}, callback=self.logged_in)]  def logged_in(self, response):     # \u043f\u0440\u043e\u0434\u043e\u043b\u0436\u0435\u043d\u0438\u0435 \u0441\u043a\u0440\u0430\u043f\u0438\u043d\u0433\u0430 \u043f\u043e\u0441\u043b\u0435 \u0432\u0445\u043e\u0434\u0430     pass<\/code><\/pre>\n<p>\u0414\u043b\u044f \u0441\u0442\u0440\u0430\u043d\u0438\u0446, \u0433\u0434\u0435 \u043a\u043e\u043d\u0442\u0435\u043d\u0442 \u0437\u0430\u0433\u0440\u0443\u0436\u0430\u0435\u0442\u0441\u044f \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e JS, \u043c\u043e\u0436\u043d\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c <a href=\"https:\/\/splash.readthedocs.io\/en\/stable\/\">Splash <\/a>\u0438\u043b\u0438 <a href=\"https:\/\/selenium-python.readthedocs.io\/\">Selenium<\/a>, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u044e\u0442 \u0438\u0441\u043f\u043e\u043b\u043d\u044f\u0442\u044c JS \u043f\u0435\u0440\u0435\u0434 \u0438\u0437\u0432\u043b\u0435\u0447\u0435\u043d\u0438\u0435\u043c \u0434\u0430\u043d\u043d\u044b\u0445. \u041d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u0442\u043e\u0442 \u0436\u0435 Splash \u0438\u043d\u0442\u0435\u0433\u0440\u0438\u0440\u0443\u0435\u0442\u0441\u044f \u0441 Scrapy \u0447\u0435\u0440\u0435\u0437 scrapy-splash, \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u044f \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0442\u044c JavaScript \u0438 \u043f\u043e\u043b\u0443\u0447\u0430\u0442\u044c \u0430\u043a\u0442\u0443\u0430\u043b\u044c\u043d\u044b\u0439 HTML. \u041f\u0440\u0438\u043c\u0435\u0440:<\/p>\n<pre><code class=\"python\">from scrapy_splash import SplashRequest  def start_requests(self):     yield SplashRequest(url='http:\/\/example.com', callback=self.parse, endpoint='render.html', args={'wait': 0.5})  def parse(self, response):     # \u0438\u0437\u0432\u043b\u0435\u0447\u0435\u043d\u0438\u0435 \u0434\u0430\u043d\u043d\u044b\u0445     pass<\/code><\/pre>\n<p>Scrapy \u043f\u043e\u0434\u0434\u0435\u0440\u0436\u0438\u0432\u0430\u0435\u0442 \u044d\u043a\u0441\u043f\u043e\u0440\u0442 \u0434\u0430\u043d\u043d\u044b\u0445 \u0432 \u0444\u043e\u0440\u043c\u0430\u0442\u044b <em>JSON<\/em>, <em>CSV<\/em> \u0438 <em>XML<\/em> \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u0432\u0441\u0442\u0440\u043e\u0435\u043d\u043d\u044b\u0445 \u044d\u043a\u0441\u043f\u043e\u0440\u0442\u0435\u0440\u043e\u0432. \u042d\u043a\u0441\u043f\u043e\u0440\u0442 \u0434\u0430\u043d\u043d\u044b\u0445 \u043c\u043e\u0436\u0435\u0442 \u0431\u044b\u0442\u044c \u043d\u0430\u0441\u0442\u0440\u043e\u0435\u043d \u0447\u0435\u0440\u0435\u0437 <code>settings.py<\/code> \u043f\u0440\u043e\u0435\u043a\u0442\u0430 \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u0430 <code>FEEDS<\/code>, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u0435\u0442 \u0443\u043a\u0430\u0437\u0430\u0442\u044c \u0440\u0430\u0437\u043b\u0438\u0447\u043d\u044b\u0435 \u043d\u0430\u0441\u0442\u0440\u043e\u0439\u043a\u0438 \u044d\u043a\u0441\u043f\u043e\u0440\u0442\u0430 \u0434\u043b\u044f \u043a\u0430\u0436\u0434\u043e\u0433\u043e \u0442\u0438\u043f\u0430 \u0444\u0430\u0439\u043b\u0430:<\/p>\n<pre><code class=\"python\"># \u043d\u0430\u0441\u0442\u0440\u043e\u0439\u043a\u0430 \u044d\u043a\u0441\u043f\u043e\u0440\u0442\u0430 \u0434\u0430\u043d\u043d\u044b\u0445 \u0432 JSON \u0438 CSV FEEDS = {     'items.json': {         'format': 'json',         'encoding': 'utf8',         'store_empty': False,         'fields': None,         'indent': 4,     },     'items.csv': {         'format': 'csv',         'fields': ['field1', 'field2', 'field3'],  # \u0441\u043f\u0435\u0446\u0438\u0444\u0438\u043a\u0430\u0446\u0438\u044f \u043f\u043e\u043b\u0435\u0439 \u0434\u043b\u044f \u044d\u043a\u0441\u043f\u043e\u0440\u0442\u0430     } }<\/code><\/pre>\n<p><strong>Item Pipelines<\/strong> \u0432 Scrapy \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u044e\u0442 \u043f\u0440\u043e\u0432\u043e\u0434\u0438\u0442\u044c \u043f\u043e\u0441\u043b\u0435\u043f\u0440\u043e\u0446\u0435\u0441\u0441\u0438\u043d\u0433 \u0441\u043e\u0431\u0440\u0430\u043d\u043d\u044b\u0445 \u0434\u0430\u043d\u043d\u044b\u0445, \u0432\u043a\u043b\u044e\u0447\u0430\u044f \u0438\u0445 \u0432\u0430\u043b\u0438\u0434\u0430\u0446\u0438\u044e, \u043e\u0447\u0438\u0441\u0442\u043a\u0443 \u0438 \u0441\u043e\u0445\u0440\u0430\u043d\u0435\u043d\u0438\u0435 \u0432 \u0411\u0414. \u041d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u0434\u043b\u044f \u0441\u043e\u0445\u0440\u0430\u043d\u0435\u043d\u0438\u044f \u0434\u0430\u043d\u043d\u044b\u0445 \u0432 \u0411\u0414 MySQL \u0438\u043b\u0438 PostgreSQL, \u043c\u043e\u0436\u043d\u043e \u043d\u0430\u0441\u0442\u0440\u043e\u0438\u0442\u044c pipeline, \u043a\u043e\u0442\u043e\u0440\u0430\u044f \u0431\u0443\u0434\u0435\u0442 \u043e\u0431\u0440\u0430\u0431\u0430\u0442\u044b\u0432\u0430\u0442\u044c \u043a\u0430\u0436\u0434\u044b\u0439 \u0441\u043e\u0431\u0440\u0430\u043d\u043d\u044b\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442:<\/p>\n<pre><code class=\"python\">import psycopg2  class PostgresPipeline(object):     def open_spider(self, spider):         hostname = 'localhost'         username = 'postgres'         password = 'yourpassword'         database = 'scrapy_db'         self.connection = psycopg2.connect(host=hostname, user=username, password=password, dbname=database)         self.cur = self.connection.cursor()      def process_item(self, item, spider):         self.cur.execute(\"INSERT INTO table_name (field1, field2) VALUES (%s, %s)\",                           (item['field1'], item['field2']))         self.connection.commit()         return item      def close_spider(self, spider):         self.cur.close()         self.connection.close()<\/code><\/pre>\n<p>\u042d\u0442\u043e\u0442 pipeline \u0443\u0441\u0442\u0430\u043d\u0430\u0432\u043b\u0438\u0432\u0430\u0435\u0442 \u0441\u043e\u0435\u0434\u0438\u043d\u0435\u043d\u0438\u0435 \u0441 \u0411\u0414 \u043f\u0440\u0438 \u043e\u0442\u043a\u0440\u044b\u0442\u0438\u0438 \u043f\u0430\u0443\u043a\u0430, \u043e\u0431\u0440\u0430\u0431\u0430\u0442\u044b\u0432\u0430\u0435\u0442 \u043a\u0430\u0436\u0434\u044b\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u043f\u0440\u0438 \u0435\u0433\u043e \u0441\u0431\u043e\u0440\u0435, \u0432\u0441\u0442\u0430\u0432\u043b\u044f\u044f \u0434\u0430\u043d\u043d\u044b\u0435 \u0432 \u0442\u0430\u0431\u043b\u0438\u0446\u0443, \u0438 \u0437\u0430\u043a\u0440\u044b\u0432\u0430\u0435\u0442 \u0441\u043e\u0435\u0434\u0438\u043d\u0435\u043d\u0438\u0435 \u043f\u043e\u0441\u043b\u0435 \u0437\u0430\u0432\u0435\u0440\u0448\u0435\u043d\u0438\u044f \u0440\u0430\u0431\u043e\u0442\u044b \u043f\u0430\u0443\u043a\u0430.<\/p>\n<h4>\u041f\u0440\u0438\u043c\u0435\u0440 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u044f<\/h4>\n<p>\u0421\u043e\u0437\u0434\u0430\u0434\u0438\u043c \u043f\u0430\u0440\u0441\u0435\u0440, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0431\u0443\u0434\u0435\u0442 \u0431\u0443\u0434\u0435\u0442 \u0441\u043e\u0431\u0438\u0440\u0430\u0442\u044c \u0434\u0430\u043d\u043d\u044b\u0435 \u0441 \u0432\u044b\u043c\u044b\u0448\u043b\u0435\u043d\u043d\u043e\u0433\u043e \u0441\u0430\u0439\u0442\u0430 \u043a\u043d\u0438\u0436\u043d\u043e\u0433\u043e \u043c\u0430\u0433\u0430\u0437\u0438\u043d\u0430, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0438\u043c\u0435\u0435\u0442 \u043f\u0430\u0433\u0438\u043d\u0430\u0446\u0438\u044e, \u0442\u0440\u0435\u0431\u0443\u0435\u0442 \u0430\u0432\u0442\u043e\u0440\u0438\u0437\u0430\u0446\u0438\u0438 \u0434\u043b\u044f \u0434\u043e\u0441\u0442\u0443\u043f\u0430 \u043a \u043d\u0435\u043a\u043e\u0442\u043e\u0440\u044b\u043c \u0441\u0442\u0440\u0430\u043d\u0438\u0446\u0430\u043c \u0438 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442 JS \u0434\u043b\u044f \u043e\u0442\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u044f \u043d\u0435\u043a\u043e\u0442\u043e\u0440\u044b\u0445 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432.<\/p>\n<p>\u0421\u043e\u0437\u0434\u0430\u0434\u0438\u043c \u043d\u043e\u0432\u044b\u0439 \u043f\u0440\u043e\u0435\u043a\u0442 Scrapy:<\/p>\n<pre><code class=\"bash\">scrapy startproject books_scraper<\/code><\/pre>\n<p>\u041f\u0435\u0440\u0435\u0445\u043e\u0434\u0438\u043c \u0432 \u0434\u0438\u0440\u0435\u043a\u0442\u043e\u0440\u0438\u044e \u043f\u0440\u043e\u0435\u043a\u0442\u0430:<\/p>\n<pre><code class=\"bash\">cd books_scraper<\/code><\/pre>\n<p>\u0412 \u0434\u0438\u0440\u0435\u043a\u0442\u043e\u0440\u0438\u0438 <code>spiders<\/code> \u0441\u043e\u0437\u0434\u0430\u0434\u0438\u043c \u043d\u043e\u0432\u043e\u0433\u043e \u043f\u0430\u0443\u043a\u0430 <code>book_spider.py<\/code>:<\/p>\n<pre><code class=\"python\">import scrapy from scrapy_splash import SplashRequest from scrapy.http import FormRequest from books_scraper.items import BookItem  # \u043d\u0443\u0436\u0435\u043d \u043a\u043b\u0430\u0441\u0441 Item \u0432 items.py  class BookSpider(scrapy.Spider):     name = 'book_spider'     login_url = 'http:\/\/books.example.com\/login'     start_urls = ['http:\/\/books.example.com']      def start_requests(self):         #\u043f\u0435\u0440\u0435\u0445\u043e\u0434 \u043d\u0430 \u0441\u0442\u0440\u0430\u043d\u0438\u0446\u0443 \u0432\u0445\u043e\u0434\u0430         yield scrapy.Request(self.login_url, self.parse_login)      def parse_login(self, response):         # \u043e\u0442\u043f\u0440\u0430\u0432\u043a\u0430 \u0434\u0430\u043d\u043d\u044b\u0445 \u0434\u043b\u044f \u0432\u0445\u043e\u0434\u0430         yield FormRequest.from_response(response,                                         formdata={'username': 'user', 'password': 'pass'},                                         callback=self.after_login)      def after_login(self, response):         # \u043f\u0440\u043e\u0432\u0435\u0440\u043a\u0430 \u0443\u0441\u043f\u0435\u0448\u043d\u043e\u0433\u043e \u0432\u0445\u043e\u0434\u0430 \u0438 \u043f\u0440\u043e\u0434\u043e\u043b\u0436\u0435\u043d\u0438\u0435 \u0441\u043a\u0440\u0430\u043f\u0438\u043d\u0433\u0430         if \"authentication failed\" in response.body:             self.logger.error(\"Login failed\")             return         else:             # \u0437\u0430\u043f\u0443\u0441\u043a \u0441\u043a\u0440\u0430\u043f\u0438\u043d\u0433\u0430 \u0441 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0435\u043c Splash \u0434\u043b\u044f \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u043a\u0438 JavaScript             for url in self.start_urls:                 yield SplashRequest(url, self.parse, endpoint='execute', args={'lua_source': self.lua_script()})      def parse(self, response):         # \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u043a\u0430 \u0434\u0430\u043d\u043d\u044b\u0445 \u0441\u0442\u0440\u0430\u043d\u0438\u0446\u044b         books = response.css('div.book')         for book in books:             item = BookItem()             item['title'] = book.css('h3.title::text').get()             item['price'] = book.css('p.price::text').get()             yield item          # \u043f\u0430\u0433\u0438\u043d\u0430\u0446\u0438\u044f         next_page = response.css('li.next a::attr(href)').get()         if next_page:             yield SplashRequest(next_page, self.parse, endpoint='execute', args={'lua_source': self.lua_script()})      def lua_script(self):         # Lua \u0441\u043a\u0440\u0438\u043f\u0442 \u0434\u043b\u044f Splash, \u0441\u043a\u0440\u043e\u043b\u043b\u0438\u043d\u0433 \u0441\u0442\u0440\u0430\u043d\u0438\u0446\u044b \u0438\u043b\u0438 \u0434\u0440\u0443\u0433\u0438\u0435 \u0434\u0435\u0439\u0441\u0442\u0432\u0438\u044f         return \"\"\"         function main(splash)             splash:set_user_agent(splash.args.ua)             assert(splash:go(splash.args.url))             assert(splash:wait(1))             return {html = splash:html()}         end         \"\"\"<\/code><\/pre>\n<p>\u0412 \u0444\u0430\u0439\u043b\u0435 <code>items.py<\/code> \u0441\u043e\u0437\u0434\u0430\u0434\u0438\u043c \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u0443 \u0434\u0430\u043d\u043d\u044b\u0445 \u0434\u043b\u044f \u043a\u043d\u0438\u0433:<\/p>\n<pre><code class=\"python\">import scrapy  class BookItem(scrapy.Item):     title = scrapy.Field()     price = scrapy.Field()<\/code><\/pre>\n<p>\u0412 <code>pipelines.py<\/code> \u0434\u043e\u0431\u0430\u0432\u0438\u043c \u043a\u043e\u0434 \u0434\u043b\u044f \u0441\u043e\u0445\u0440\u0430\u043d\u0435\u043d\u0438\u044f \u0434\u0430\u043d\u043d\u044b\u0445 \u0432 PostgreSQL:<\/p>\n<pre><code class=\"python\">import psycopg2  class PostgresPipeline(object):     def open_spider(self, spider):         self.connection = psycopg2.connect(host='localhost', dbname='scrapy_books', user='postgres', password='password')         self.cur = self.connection.cursor()      def close_spider(self, spider):         self.connection.close()      def process_item(self, item, spider):         self.cur.execute(\"INSERT INTO books (title, price) VALUES (%s, %s)\", (item['title'], item['price']))         self.connection.commit()         return item<\/code><\/pre>\n<p>\u0410\u043a\u0442\u0438\u0432\u0438\u0440\u0443\u0435\u043c <code>PostgresPipeline<\/code> \u0432 <code>settings.py<\/code> \u0438 \u043d\u0430\u0441\u0442\u0440\u0430\u0438\u0432\u0435\u043c Splash \u0432 \u0441\u043b\u0443\u0447\u0430\u0435 \u0440\u0430\u0431\u043e\u0442\u044b \u0441 JS:<\/p>\n<pre><code class=\"python\">ITEM_PIPELINES = {    'books_scraper.pipelines.PostgresPipeline': 300, }  SPLASH_URL = 'http:\/\/localhost:8050'<\/code><\/pre>\n<p>\u0417\u0430\u043f\u0443\u0441\u043a\u0430\u0435\u043c:<\/p>\n<pre><code class=\"bash\">scrapy crawl book_spider<\/code><\/pre>\n<hr\/>\n<p>\u041f\u043e\u0434\u0440\u043e\u0431\u043d\u0435\u0439 \u0441 Scrapy \u043c\u043e\u0436\u043d\u043e <a href=\"https:\/\/docs.scrapy.org\/en\/latest\/\">\u043e\u0437\u043d\u0430\u043a\u043e\u043c\u0438\u0442\u044c\u0441\u044f \u0432 \u0434\u043e\u043a\u0443\u043c\u0435\u043d\u0442\u0430\u0446\u0438\u0438.<\/a><\/p>\n<p>\u0412 \u0437\u0430\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u0435 \u0440\u0430\u0441\u0441\u043a\u0430\u0436\u0443 \u043e\u0431 \u043e\u0442\u043a\u0440\u044b\u0442\u044b\u0445 \u0443\u0440\u043e\u043a\u0430\u0445:<\/p>\n<ul>\n<li>\n<p>13 \u043c\u0430\u044f: \u0413\u0435\u043d\u0435\u0440\u0430\u0442\u043e\u0440\u044b \u0438 \u043e\u0447\u0435\u0440\u0435\u0434\u0438 \u0432 Python: \u0440\u0430\u0441\u0441\u043c\u043e\u0442\u0440\u0438\u043c \u043a\u043e\u043d\u0446\u0435\u043f\u0446\u0438\u0438 \u0433\u0435\u043d\u0435\u0440\u0430\u0442\u043e\u0440\u043d\u044b\u0445 \u0444\u0443\u043d\u043a\u0446\u0438\u0439 \u0438 \u043e\u0447\u0435\u0440\u0435\u0434\u0435\u0439, \u043f\u043e\u0433\u043e\u0432\u043e\u0440\u0438\u043c \u043e \u0441\u0445\u043e\u0434\u0441\u0442\u0432\u0430\u0445, \u0440\u0430\u0437\u043b\u0438\u0447\u0438\u044f\u0445, \u0438 \u043f\u0440\u0438\u043c\u0435\u043d\u0438\u043c\u043e\u0441\u0442\u0438 \u0432 \u0440\u0435\u0448\u0435\u043d\u0438\u0438 \u0440\u0435\u0430\u043b\u044c\u043d\u044b\u0445 \u0437\u0430\u0434\u0430\u0447. <a href=\"https:\/\/otus.pw\/Yhze\/\">\u0417\u0430\u043f\u0438\u0441\u0430\u0442\u044c\u0441\u044f<\/a><\/p>\n<\/li>\n<li>\n<p>28 \u043c\u0430\u044f: \u0421\u0430\u043c\u044b\u0435 \u044d\u0444\u0444\u0435\u043a\u0442\u0438\u0432\u043d\u044b\u0435 \u043e\u0434\u043d\u043e\u0441\u0442\u0440\u043e\u0447\u043d\u0438\u043a\u0438 \u0432 Python: \u043f\u043e\u0434\u0435\u043b\u0438\u043c\u0441\u044f \u0441\u0435\u043a\u0440\u0435\u0442\u0430\u043c\u0438 \u0438 \u043c\u0430\u0441\u0442\u0435\u0440\u0441\u0442\u0432\u043e\u043c \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u044f \u043c\u043e\u0449\u043d\u044b\u0445 \u043e\u0434\u043d\u043e\u0441\u0442\u0440\u043e\u0447\u043d\u0438\u043a\u043e\u0432 \u0432 Python. <a href=\"https:\/\/otus.pw\/Fqy2\/\">\u0417\u0430\u043f\u0438\u0441\u0430\u0442\u044c\u0441\u044f<\/a><\/p>\n<\/li>\n<\/ul>\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\/articles\/812035\/\"> https:\/\/habr.com\/ru\/articles\/812035\/<\/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-375313","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/375313","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=375313"}],"version-history":[{"count":0,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/375313\/revisions"}],"wp:attachment":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=375313"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=375313"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=375313"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}