-
Notifications
You must be signed in to change notification settings - Fork 120
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add example script about setting and getting local storage state
- Loading branch information
Showing
1 changed file
with
40 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
from scrapy import Spider, Request | ||
from scrapy.crawler import CrawlerProcess | ||
from scrapy_playwright.page import PageCoroutine | ||
|
||
|
||
class StorageSpider(Spider): | ||
""" | ||
Set and get storage state | ||
""" | ||
name = "storage" | ||
|
||
def start_requests(self): | ||
yield Request( | ||
url="https://example.org", | ||
meta={ | ||
"playwright": True, | ||
"playwright_include_page": True, | ||
"playwright_page_coroutines": [ | ||
PageCoroutine("evaluate_handle", "window.localStorage.setItem('foo', 'bar');"), | ||
], | ||
}, | ||
) | ||
|
||
async def parse(self, response): | ||
page = response.meta["playwright_page"] | ||
return {"url": response.url, "storage_state": await page.context.storage_state()} | ||
|
||
|
||
if __name__ == "__main__": | ||
process = CrawlerProcess( | ||
settings={ | ||
"TWISTED_REACTOR": "twisted.internet.asyncioreactor.AsyncioSelectorReactor", | ||
"DOWNLOAD_HANDLERS": { | ||
"https": "scrapy_playwright.handler.ScrapyPlaywrightDownloadHandler", | ||
# "http": "scrapy_playwright.handler.ScrapyPlaywrightDownloadHandler", | ||
}, | ||
} | ||
) | ||
process.crawl(StorageSpider) | ||
process.start() |