Apify & CrawleeA&CApify & Crawlee
Powered by
hurt-tomatoH
Apify & Crawlee•15mo ago•
15 replies
hurt-tomato

infinite scrolling - best practice

I'm crawling a products page on an online toy store. There is an 'all products' page that loads 20 products at a time. It's an infinite scrolling scenario so no pagination buttons. I followed the tutorial from the blog and added the await for netowrkidle state and infinite_scroll method to implement the page to be scrolled continuously. After each scroll, 20 more products are loaded and the process repeats. The issue I'm facing is that there are thousands of products to retrieve. So, eventually a timeout error is thrown.

To me, this approach seems rather inefficient. Not only is the timeout an issue (which I'm sure can be adjusted), but since it might take an hour to scroll all products any random event (popup, or some anti-scrape challenge) might appear and end the run before the enqueued product detail links can even be processed.

I get that using API requests, where possible is preferable but I haven't identified those yet. I do see that after each scroll, the URL is appended with ?page=2. So scroll down again and it become ?page=3, etc.

Would would be best practice to implement this cleanly within the Crawlee framework? Any pointers from you gurus? Thanks!


Below is the default handler implementing the infinite scroll. I'm enqueuing all the product detail links which are then processed by a separate route handler.

@router.default_handler
async def default_handler(context: PlaywrightCrawlingContext) -> None:
context.log.info(f'default_handler is processing: {context.request.url}')

await context.page.wait_for_selector('.ais-InfiniteHits-list')

# Infinite scrolling: wait for page load to complete and scroll to bottom
await context.page.wait_for_load_state('networkidle')
await context.infinite_scroll()

await context.enqueue_links(
selector='.ais-InfiniteHits-item .result-wrapper div a',
label='PRODUCT',
unique_key=context.request.url
)
Apify & Crawlee banner
Apify & CrawleeJoin
This is the official developer community of Apify and Crawlee.
14,091Members
Resources
Recent Announcements

Similar Threads

Was this page helpful?
Recent Announcements
ellativity

**Update to Store Publishing Terms and Acceptable Use Policy** Due to an influx of fraudulent reviews recently, Apify's Legal team has taken some actions to protect developers, customers, and Apify, by updating the Store Publishing Terms and Acceptable Use Policy. Please pay special attention to the updated terms in section 4 of the Store Publishing Terms here: https://docs.apify.com/legal/store-publishing-terms-and-conditions Additionally, please review the changes to section 2 of the Acceptable Use Policy here: https://docs.apify.com/legal/acceptable-use-policy If you have any questions, please ask them in <#1206131794261315594> so everyone can see the discussion. Thanks!

ellativity · 3w ago

ellativity

Hi @everyone I'm hanging out with the Creator team at Apify in https://discord.com/channels/801163717915574323/1430491198145167371 if you want to discuss Analytics and Insights!

ellativity · 4w ago

ellativity

2 things for <@&1092713625141137429> members today: 1. The Apify developer rewards program is open for registrations: https://apify.notion.site/developer-rewards This is the program where you will earn points for marketing activities. The rewards are still TBC, but the real purpose of the program is to help you structure your marketing activities and efforts. In the coming weeks, I will be populating that link with guides to help you identify the best ways to market your Actors, as well as scheduling workshops and office hours to help you create content and develop your own marketing strategy. 2. At 2PM CET (in about 80 minutes) there will be an office hour with the team behind Insights and Analytics, who want your feedback on how to improve analytics for you. Join us in https://discord.com/channels/801163717915574323/1430491198145167371 to share your ideas!

ellativity · 4w ago

Similar Threads

Error handling / best practices
verbal-limeVverbal-lime / crawlee-python
2y ago
infinite_scroll | how to get the updated page
improved-purpleIimproved-purple / crawlee-python
2y ago
How to use proxies with Playwright? And, what are the best proxy service providers?
living-lavenderLliving-lavender / crawlee-python
2y ago
clean way to stop "request queue seems to be stuck for 300.0"
moderate-tomatoMmoderate-tomato / crawlee-python
14mo ago