From fa98add7b6463a6b2a22338d16432f3ffc3cc6d2 Mon Sep 17 00:00:00 2001 From: AndrewKorzh <92707967+AndrewKorzh@users.noreply.github.com> Date: Mon, 12 Aug 2024 12:16:18 +0300 Subject: [PATCH] add examples --- examples/settings.py | 2 ++ examples/spiders/form_action.py | 46 +++++++++++++++++++++++++++++++++ examples/spiders/har.py | 26 +++++++++++++++++++ 3 files changed, 74 insertions(+) create mode 100644 examples/spiders/form_action.py create mode 100644 examples/spiders/har.py diff --git a/examples/settings.py b/examples/settings.py index bdfcff7..47670b5 100644 --- a/examples/settings.py +++ b/examples/settings.py @@ -10,3 +10,5 @@ } PUPPETEER_SERVICE_URL = "http://localhost:3000" + +PUPPETEER_LOCAL = False \ No newline at end of file diff --git a/examples/spiders/form_action.py b/examples/spiders/form_action.py new file mode 100644 index 0000000..18b1329 --- /dev/null +++ b/examples/spiders/form_action.py @@ -0,0 +1,46 @@ +import scrapy +from scrapypuppeteer import PuppeteerRequest, PuppeteerScreenshotResponse +from scrapypuppeteer.actions import Screenshot, FormAction +import base64 + +class FormActionSpider(scrapy.Spider): + name = 'form_action' + start_urls = ["https://www.roboform.com/filling-test-all-fields"] + + def start_requests(self): + for url in self.start_urls: + yield PuppeteerRequest(url, callback=self.form_action, close_page=False) + + def form_action(self, response): + input_mapping = { + 'input[name="02frstname"]': { + "value": "SomeName", + "delay": 50 + }, + 'input[name="05_company"]': { + "value": "SomeCompany", + "delay": 100 + }, + 'input[name="06position"]': { + "value": "SomePosition", + "delay": 100 + } + } + + yield response.follow( + FormAction(input_mapping), + close_page=False, + callback=self.screenshot + ) + + def screenshot(self, response): + action = Screenshot(options = {"fullPage": True,}) + yield response.follow(action, callback = self.make_screenshot, close_page = False) + + @staticmethod + def make_screenshot(response: PuppeteerScreenshotResponse, **kwargs): + data = ( + response.screenshot + ) + with open(f"screenshot.png", "wb") as fh: + fh.write(base64.b64decode(data)) diff --git a/examples/spiders/har.py b/examples/spiders/har.py new file mode 100644 index 0000000..ab9a4c1 --- /dev/null +++ b/examples/spiders/har.py @@ -0,0 +1,26 @@ +import scrapy +from scrapypuppeteer import PuppeteerRequest +from scrapypuppeteer.actions import Har + + +def write_to_file(file_path, content): + with open(file_path, 'a', encoding='utf-8') as file: + file.write(content) + +class HarSpider(scrapy.Spider): + name = 'har' + start_urls = ["https://github.com/pyppeteer/pyppeteer"] + + def start_requests(self): + for url in self.start_urls: + yield PuppeteerRequest(url, callback=self.har, close_page=False, har_recording=True) + + def har(self, response): + yield response.follow( + Har(), + close_page=False, + callback=self.save_har, + ) + + def save_har(self, response): + write_to_file("result.har", response.har)