Skip to main content

Step-by-Step Guide to Using Scrapy with Playwright

Published 2 min readMarkdown
Part of seriesMastering Web Scraping with Scrapy: From Zero to Hero

Playwright is a newer, faster, and more reliable browser automation tool than Selenium. Integrating it with Scrapy is often preferred for modern web scraping projects.

Why Playwright?

  • Faster: Generally faster execution than Selenium.

  • Better Waiting: Auto-waits for elements to be ready.

  • Modern Web Support: Better handling of modern web features.

Setup

We will use the scrapy-playwright plugin, which makes integration seamless.

  1. Install the package:

     pip install scrapy-playwright
     playwright install
    

Configuration

Update your settings.py to enable the scrapy-playwright download handler:

# settings.py

DOWNLOAD_HANDLERS = {
    "http": "scrapy_playwright.handler.ScrapyPlaywrightDownloadHandler",
    "https": "scrapy_playwright.handler.ScrapyPlaywrightDownloadHandler",
}

TWISTED_REACTOR = "twisted.internet.asyncioreactor.AsyncioSelectorReactor"

Using Playwright in Your Spider

To use Playwright for a request, you simply need to pass meta={"playwright": True}.

# spiders/playwright_spider.py
import scrapy


class PlaywrightSpider(scrapy.Spider):
    name = "playwright_spider"

    def start_requests(self):
        yield scrapy.Request(
            url="https://example.com/dynamic",
            meta={"playwright": True},
            callback=self.parse
        )

    def parse(self, response):
        # The response is now the rendered HTML from Playwright
        yield {
            "text": response.css("div.content::text").get()
        }

Advanced Usage: Page Interactions

You can also interact with the page using playwright_page_methods.

from scrapy_playwright.page import PageMethod


def start_requests(self):
    yield scrapy.Request(
        url="https://example.com/login",
        meta={
            "playwright": True,
            "playwright_page_methods": [
                PageMethod("fill", "input[name='user']", "myuser"),
                PageMethod("fill", "input[name='pass']", "mypass"),
                PageMethod("click", "button[type='submit']"),
                PageMethod("wait_for_selector", "div.dashboard"),
            ],
        },
        callback=self.parse_dashboard
    )

Comparison with Selenium Integration

FeatureScrapy + SeleniumScrapy + Playwright
SetupManual MiddlewarePlugin (scrapy-playwright)
SpeedSlowerFaster
Ease of UseModerateEasy (with plugin)
ReliabilityGoodExcellent

Conclusion

For new projects requiring JavaScript rendering, Scrapy + Playwright is the recommended approach due to its performance and ease of integration.

Next Steps

In the next article, we will discuss how to debug Scrapy spiders effectively.

About the author

  • I’m Ravikirana B – an engineer driven by curiosity and clarity. My work sits at the intersection of hardware and software. I specialize in Python programming and electronics, building real-world solutions that don’t just work—they make sense. I started 'Tech Priya' with a simple mission: to share the joy of technology. "Priya" means dear or beloved, and this platform is dedicated to everyone who loves to understand the "why" and "how" behind the machines we use every day. What you’ll find here: 🔌 Electronics Simplified: Complex circuits explained with relatable analogies (think water tanks, gates, and traffic flows). 🐍 Python in Practice: Automation ideas, coding insights, and tool development. 💡 Real Reflections: Honest takes on tech, bridging the gap between textbook theory and hands-on reality. 🌿 Native Connection: Tech concepts explained with a Kannada-English touch to make learning feel like home. I believe technology shouldn't be a barrier. Whether you are a student from a small town or a self-learner with big dreams, Tech Priya is here to make the complex simple. Let’s keep exploring—clearly, curiously, and together. 🙌

No comments yet. Be the first to comment.

More from this blog