Skip to content

How to stop crawling a web site when a goal is reached #503

Closed

Description

@sebpiq

I have a list of web sites from which I am trying to scrape a given piece of info. For each site, once I have found that info, I want to stop and move on to the next (with several sites being scraped concurrently).

I have tried the following approach (emptying the request queue when my goal is found) :

request_queue = await RequestQueue.open()
crawler = PlaywrightCrawler(
    request_provider=request_queue,
    headless=True,  # Show the browser window.
    browser_type='firefox',  # Use the Firefox browser.
)
    
await crawler.add_requests([root_url])

@crawler.router.default_handler
async def request_handler(context: PlaywrightCrawlingContext) -> None:
    # ...
    if found:
        await request_queue.drop()

But that's actually raising an error :

 ValueError: Request queue with id "default" does not exist.

Any idea how I should proceed to have a finer control over the request queue ? Thanks !

Activity

  1. added
    t-tooling
    on Sep 6, 2024
  2. sebpiq commented on Sep 6, 2024

    @sebpiq
    Author

    Also, should RequestQueue have a delete_request function to allow removing requests from the queue when they're not needed anymore?

  3. gladwindos commented on Sep 7, 2024

    @gladwindos

    You may need to pass a unique name into RequestQueue like so:

    request_queue = await RequestQueue.open(name=str(uuid.uuid4()))

    I was having a similar problem and found this issue helpful: #351

  4. locked and limited conversation to collaborators on Sep 9, 2024
  5. converted this issue into a discussion #506 on Sep 9, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    t-tooling

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions