-
Notifications
You must be signed in to change notification settings - Fork 129
/
Copy pathevents.py
41 lines (34 loc) · 1.39 KB
/
events.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
39
40
41
from playwright.async_api import Dialog, Response as PlaywrightResponse
from scrapy import Spider, Request
from scrapy_playwright.page import PageMethod
class EventsSpider(Spider):
"""Handle page events."""
name = "events"
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_page_methods": [
PageMethod("evaluate", "alert('foobar');"),
],
"playwright_page_event_handlers": {
"dialog": self.handle_dialog,
"response": "handle_response",
},
},
)
async def handle_dialog(self, dialog: Dialog) -> None:
self.logger.info(f"Handled dialog with message: {dialog.message}")
await dialog.dismiss()
async def handle_response(self, response: PlaywrightResponse) -> None:
self.logger.info(f"Received response with URL {response.url}")
def parse(self, response, **kwargs):
return {"url": response.url}