-
Notifications
You must be signed in to change notification settings - Fork 121
/
Copy pathmax_pages.py
50 lines (44 loc) · 1.65 KB
/
max_pages.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
42
43
44
45
46
47
48
49
50
from playwright.async_api import Page
from scrapy import Spider, Request
class MaxPagesPerContextContextsSpider(Spider):
"""Limit pages by context"""
name = "contexts"
custom_settings = {
"TWISTED_REACTOR": "twisted.internet.asyncioreactor.AsyncioSelectorReactor",
"DOWNLOAD_HANDLERS": {
"https": "scrapy_playwright.handler.ScrapyPlaywrightDownloadHandler",
# "http": "scrapy_playwright.handler.ScrapyPlaywrightDownloadHandler",
},
"PLAYWRIGHT_MAX_PAGES_PER_CONTEXT": 2,
"PLAYWRIGHT_CONTEXTS": {
"a": {"java_script_enabled": True},
"b": {"java_script_enabled": True},
},
}
def start_requests(self):
for _ in range(20):
yield Request(
url="https://httpbin.org/status?n=404",
meta={
"playwright": True,
"playwright_context": "a",
"playwright_include_page": True,
},
dont_filter=True,
errback=self.errback,
)
for i in range(20):
yield Request(
url=f"https://httpbin.org/get?a={i}",
meta={"playwright": True, "playwright_context": "a"},
)
for i in range(20):
yield Request(
url=f"https://httpbin.org/get?b={i}",
meta={"playwright": True, "playwright_context": "b"},
)
def parse(self, response, **kwargs):
return {"url": response.url}
async def errback(self, failure):
page: Page = failure.request.meta["playwright_page"]
await page.close()