-
Notifications
You must be signed in to change notification settings - Fork 121
/
Copy pathstorage.py
38 lines (33 loc) · 1.25 KB
/
storage.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
from playwright.async_api import Page
from scrapy import Spider, Request
from scrapy_playwright.page import PageMethod
class StorageSpider(Spider):
"""Set and get storage state, get the server's IP address."""
name = "storage"
custom_settings = {
"TWISTED_REACTOR": "twisted.internet.asyncioreactor.AsyncioSelectorReactor",
"DOWNLOAD_HANDLERS": {
"https": "scrapy_playwright.handler.ScrapyPlaywrightDownloadHandler",
# "http": "scrapy_playwright.handler.ScrapyPlaywrightDownloadHandler",
},
}
def start_requests(self):
yield Request(
url="https://example.org",
meta={
"playwright": True,
"playwright_include_page": True,
"playwright_page_methods": [
PageMethod("evaluate_handle", "window.localStorage.setItem('foo', 'bar');"),
],
},
)
async def parse(self, response, **kwargs):
page: Page = response.meta["playwright_page"]
storage_state = await page.context.storage_state()
await page.close()
return {
"url": response.url,
"storage_state": storage_state,
"ip_address": response.ip_address,
}