scrapy-plugins / scrapy-playwright

🎭 Playwright integration for Scrapy
BSD 3-Clause "New" or "Revised" License
1.03k stars 113 forks source link

KeyError: 'playwright_page' #231

Closed expecttheunusual closed 1 year ago

expecttheunusual commented 1 year ago

Windows 10

# spiders/quotes.py

import scrapy
# from quotes_js_scraper.items import QuoteItem
from scrapy_playwright.page import PageMethod

class QuotesSpider(scrapy.Spider):
    name = 'quotes'

    def start_requests(self):
        url = "https://quotes.toscrape.com/js/"
        yield scrapy.Request(url, meta=dict(
                playwright = True,
                playwright_include_page = True, 
                playwright_page_methods =[PageMethod('wait_for_selector', 'div.quote')],
        errback=self.errback,
            ))

    async def parse(self, response):
        page = response.meta["playwright_page"]
        await page.close()
    async def errback(self, failure):
        page = failure.request.meta["playwright_page"]
        await page.close()
2023-09-09 13:31:06 [scrapy.utils.log] INFO: Scrapy 2.10.1 started (bot: godamin)
2023-09-09 13:31:06 [scrapy.utils.log] INFO: Versions: lxml 4.9.2.0, libxml2 2.9.12, cssselect 1.2.0, parsel 1.8.1, w3lib 2.1.2, Twisted 22.10.0, Python 3.11.3 (tags/v3.11.3:f3909b8, Apr  4 2023, 23:49:59) [MSC v.1934 64 bit (AMD64)], pyOpenSSL 23.2.0 (OpenSSL 3.1.2 1 Aug 2023), cryptography 41.0.3, Platform Windows-10-10.0.15063-SP0
2023-09-09 13:31:06 [scrapy.addons] INFO: Enabled addons:
[]
2023-09-09 13:31:06 [scrapy.crawler] INFO: Overridden settings:
{'BOT_NAME': 'godamin',
 'FEED_EXPORT_ENCODING': 'utf-8',
 'NEWSPIDER_MODULE': 'godamin.spiders',
 'REQUEST_FINGERPRINTER_IMPLEMENTATION': '2.7',
 'SPIDER_MODULES': ['godamin.spiders'],
 'TWISTED_REACTOR': 'twisted.internet.asyncioreactor.AsyncioSelectorReactor'}
2023-09-09 13:31:06 [asyncio] DEBUG: Using selector: SelectSelector
2023-09-09 13:31:06 [scrapy.utils.log] DEBUG: Using reactor: twisted.internet.asyncioreactor.AsyncioSelectorReactor
2023-09-09 13:31:06 [scrapy.utils.log] DEBUG: Using asyncio event loop: asyncio.windows_events._WindowsSelectorEventLoop
2023-09-09 13:31:06 [scrapy.extensions.telnet] INFO: Telnet Password: 48ac3a598ac28c86
2023-09-09 13:31:07 [scrapy.middleware] INFO: Enabled extensions:
['scrapy.extensions.corestats.CoreStats',
 'scrapy.extensions.telnet.TelnetConsole',
 'scrapy.extensions.logstats.LogStats']
2023-09-09 13:31:07 [scrapy.middleware] INFO: Enabled downloader middlewares:
['scrapy.downloadermiddlewares.httpauth.HttpAuthMiddleware',
 'scrapy.downloadermiddlewares.downloadtimeout.DownloadTimeoutMiddleware',
 'scrapy.downloadermiddlewares.defaultheaders.DefaultHeadersMiddleware',
 'scrapy.downloadermiddlewares.useragent.UserAgentMiddleware',
 'scrapy.downloadermiddlewares.retry.RetryMiddleware',
 'scrapy.downloadermiddlewares.redirect.MetaRefreshMiddleware',
 'scrapy.downloadermiddlewares.httpcompression.HttpCompressionMiddleware',
 'scrapy.downloadermiddlewares.redirect.RedirectMiddleware',
 'scrapy.downloadermiddlewares.cookies.CookiesMiddleware',
 'scrapy.downloadermiddlewares.httpproxy.HttpProxyMiddleware',
 'scrapy.downloadermiddlewares.stats.DownloaderStats']
2023-09-09 13:31:07 [scrapy.middleware] INFO: Enabled spider middlewares:
['scrapy.spidermiddlewares.httperror.HttpErrorMiddleware',
 'scrapy.spidermiddlewares.offsite.OffsiteMiddleware',
 'scrapy.spidermiddlewares.referer.RefererMiddleware',
 'scrapy.spidermiddlewares.urllength.UrlLengthMiddleware',
 'scrapy.spidermiddlewares.depth.DepthMiddleware']
2023-09-09 13:31:07 [scrapy.middleware] INFO: Enabled item pipelines:
[]
2023-09-09 13:31:07 [scrapy.core.engine] INFO: Spider opened
2023-09-09 13:31:07 [scrapy.extensions.logstats] INFO: Crawled 0 pages (at 0 pages/min), scraped 0 items (at 0 items/min)
2023-09-09 13:31:07 [scrapy.extensions.telnet] INFO: Telnet console listening on 127.0.0.1:6023
2023-09-09 13:31:09 [scrapy.core.engine] DEBUG: Crawled (200) <GET https://quotes.toscrape.com/js/> (referer: None)
2023-09-09 13:31:09 [scrapy.core.scraper] ERROR: Spider error processing <GET https://quotes.toscrape.com/js/> (referer: None)
Traceback (most recent call last):
  File "C:\Users\Administrator\AppData\Roaming\Python\Python311\site-packages\twisted\internet\defer.py", line 1065, in adapt
    extracted = result.result()
                ^^^^^^^^^^^^^^^
  File "C:\Users\Administrator\scrapy\project2\godamin\spiders\quotes.py", line 20, in parse
    page = response.meta["playwright_page"]
           ~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^
KeyError: 'playwright_page'
2023-09-09 13:31:09 [scrapy.core.engine] INFO: Closing spider (finished)
2023-09-09 13:31:09 [scrapy.statscollectors] INFO: Dumping Scrapy stats:
{'downloader/request_bytes': 223,
 'downloader/request_count': 1,
 'downloader/request_method_count/GET': 1,
 'downloader/response_bytes': 5982,
 'downloader/response_count': 1,
 'downloader/response_status_count/200': 1,
 'elapsed_time_seconds': 2.137,
 'finish_reason': 'finished',
 'finish_time': datetime.datetime(2023, 9, 9, 5, 31, 9, 734147),
 'log_count/DEBUG': 4,
 'log_count/ERROR': 1,
 'log_count/INFO': 10,
 'response_received_count': 1,
 'scheduler/dequeued': 1,
 'scheduler/dequeued/memory': 1,
 'scheduler/enqueued': 1,
 'scheduler/enqueued/memory': 1,
 'spider_exceptions/KeyError': 1,
 'start_time': datetime.datetime(2023, 9, 9, 5, 31, 7, 597147)}
2023-09-09 13:31:09 [scrapy.core.engine] INFO: Spider closed (finished)

C:\Users\Administrator\scrapy\project2>
elacuesta commented 1 year ago

The handler is not enabled, notice how there are no log messages from the scrapy-playwright logger ("Starting download handler", "Launching browser", etc). See the Activation section for details. Furthermore, you seem to be using Windows, be aware of the lack of native support.