-
-
Notifications
You must be signed in to change notification settings - Fork 6.5k
Expand file tree
/
Copy pathvirtual_scroll_example.py
More file actions
367 lines (293 loc) · 13.3 KB
/
virtual_scroll_example.py
File metadata and controls
367 lines (293 loc) · 13.3 KB
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
"""
Example of using the virtual scroll feature to capture content from pages
with virtualized scrolling (like Twitter, Instagram, or other infinite scroll feeds).
This example demonstrates virtual scroll with a local test server serving
different types of scrolling behaviors from HTML files in the assets directory.
"""
import asyncio
import os
import http.server
import socketserver
import threading
from pathlib import Path
from crawl4ai import AsyncWebCrawler, CrawlerRunConfig, VirtualScrollConfig, CacheMode, BrowserConfig
# Get the assets directory path
ASSETS_DIR = Path(__file__).parent / "assets"
class TestServer:
"""Simple HTTP server to serve our test HTML files"""
def __init__(self, port=8080):
self.port = port
self.httpd = None
self.server_thread = None
async def start(self):
"""Start the test server"""
Handler = http.server.SimpleHTTPRequestHandler
# Save current directory and change to assets directory
self.original_cwd = os.getcwd()
os.chdir(ASSETS_DIR)
# Try to find an available port
for _ in range(10):
try:
self.httpd = socketserver.TCPServer(("", self.port), Handler)
break
except OSError:
self.port += 1
if self.httpd is None:
raise RuntimeError("Could not find available port")
self.server_thread = threading.Thread(target=self.httpd.serve_forever)
self.server_thread.daemon = True
self.server_thread.start()
# Give server time to start
await asyncio.sleep(0.5)
print(f"Test server started on http://localhost:{self.port}")
return self.port
def stop(self):
"""Stop the test server"""
if self.httpd:
self.httpd.shutdown()
# Restore original directory
if hasattr(self, 'original_cwd'):
os.chdir(self.original_cwd)
async def example_twitter_like_virtual_scroll():
"""
Example 1: Twitter-like virtual scroll where content is REPLACED.
This is the classic virtual scroll use case - only visible items exist in DOM.
"""
print("\n" + "="*60)
print("EXAMPLE 1: Twitter-like Virtual Scroll")
print("="*60)
server = TestServer()
port = await server.start()
try:
# Configure virtual scroll for Twitter-like timeline
virtual_config = VirtualScrollConfig(
container_selector="#timeline", # The scrollable container
scroll_count=50, # Scroll up to 50 times to get all content
scroll_by="container_height", # Scroll by container's height
wait_after_scroll=0.3 # Wait 300ms after each scroll
)
config = CrawlerRunConfig(
virtual_scroll_config=virtual_config,
cache_mode=CacheMode.BYPASS
)
# TIP: Set headless=False to watch the scrolling happen!
browser_config = BrowserConfig(
headless=False,
viewport={"width": 1280, "height": 800}
)
async with AsyncWebCrawler(config=browser_config) as crawler:
result = await crawler.arun(
url=f"http://localhost:{port}/virtual_scroll_twitter_like.html",
config=config
)
# Count tweets captured
import re
tweets = re.findall(r'data-tweet-id="(\d+)"', result.html)
unique_tweets = sorted(set(int(id) for id in tweets))
print(f"\n📊 Results:")
print(f" Total HTML length: {len(result.html):,} characters")
print(f" Tweets captured: {len(unique_tweets)} unique tweets")
if unique_tweets:
print(f" Tweet IDs range: {min(unique_tweets)} to {max(unique_tweets)}")
print(f" Expected range: 0 to 499 (500 tweets total)")
if len(unique_tweets) == 500:
print(f" ✅ SUCCESS! All tweets captured!")
else:
print(f" ⚠️ Captured {len(unique_tweets)}/500 tweets")
finally:
server.stop()
async def example_traditional_append_scroll():
"""
Example 2: Traditional infinite scroll where content is APPENDED.
No virtual scroll needed - all content stays in DOM.
"""
print("\n" + "="*60)
print("EXAMPLE 2: Traditional Append-Only Scroll")
print("="*60)
server = TestServer()
port = await server.start()
try:
# Configure virtual scroll
virtual_config = VirtualScrollConfig(
container_selector=".posts-container",
scroll_count=15, # Less scrolls needed since content accumulates
scroll_by=500, # Scroll by 500 pixels
wait_after_scroll=0.4
)
config = CrawlerRunConfig(
virtual_scroll_config=virtual_config,
cache_mode=CacheMode.BYPASS
)
async with AsyncWebCrawler() as crawler:
result = await crawler.arun(
url=f"http://localhost:{port}/virtual_scroll_append_only.html",
config=config
)
# Count posts
import re
posts = re.findall(r'data-post-id="(\d+)"', result.html)
unique_posts = sorted(set(int(id) for id in posts))
print(f"\n📊 Results:")
print(f" Total HTML length: {len(result.html):,} characters")
print(f" Posts captured: {len(unique_posts)} unique posts")
if unique_posts:
print(f" Post IDs range: {min(unique_posts)} to {max(unique_posts)}")
print(f" ℹ️ Note: This page appends content, so virtual scroll")
print(f" just helps trigger more loads. All content stays in DOM.")
finally:
server.stop()
async def example_instagram_grid():
"""
Example 3: Instagram-like grid with virtual scroll.
Grid layout where only visible rows are rendered.
"""
print("\n" + "="*60)
print("EXAMPLE 3: Instagram Grid Virtual Scroll")
print("="*60)
server = TestServer()
port = await server.start()
try:
# Configure for grid layout
virtual_config = VirtualScrollConfig(
container_selector=".feed-container", # Container with the grid
scroll_count=100, # Many scrolls for 999 posts
scroll_by="container_height",
wait_after_scroll=0.2 # Faster scrolling for grid
)
config = CrawlerRunConfig(
virtual_scroll_config=virtual_config,
cache_mode=CacheMode.BYPASS,
screenshot=True # Take a screenshot of the final grid
)
# Show browser for this visual example
browser_config = BrowserConfig(
headless=False,
viewport={"width": 1200, "height": 900}
)
async with AsyncWebCrawler(config=browser_config) as crawler:
result = await crawler.arun(
url=f"http://localhost:{port}/virtual_scroll_instagram_grid.html",
config=config
)
# Count posts in grid
import re
posts = re.findall(r'data-post-id="(\d+)"', result.html)
unique_posts = sorted(set(int(id) for id in posts))
print(f"\n📊 Results:")
print(f" Posts in grid: {len(unique_posts)} unique posts")
if unique_posts:
print(f" Post IDs range: {min(unique_posts)} to {max(unique_posts)}")
print(f" Expected: 0 to 998 (999 posts total)")
# Save screenshot
if result.screenshot:
import base64
with open("instagram_grid_result.png", "wb") as f:
f.write(base64.b64decode(result.screenshot))
print(f" 📸 Screenshot saved as instagram_grid_result.png")
finally:
server.stop()
async def example_mixed_content():
"""
Example 4: News feed with mixed behavior.
Featured articles stay (no virtual scroll), regular articles are virtualized.
"""
print("\n" + "="*60)
print("EXAMPLE 4: News Feed with Mixed Behavior")
print("="*60)
server = TestServer()
port = await server.start()
try:
# Configure virtual scroll
virtual_config = VirtualScrollConfig(
container_selector="#newsContainer",
scroll_count=25,
scroll_by="container_height",
wait_after_scroll=0.3
)
config = CrawlerRunConfig(
virtual_scroll_config=virtual_config,
cache_mode=CacheMode.BYPASS
)
async with AsyncWebCrawler() as crawler:
result = await crawler.arun(
url=f"http://localhost:{port}/virtual_scroll_news_feed.html",
config=config
)
# Count different types of articles
import re
featured = re.findall(r'data-article-id="featured-\d+"', result.html)
regular = re.findall(r'data-article-id="article-(\d+)"', result.html)
print(f"\n📊 Results:")
print(f" Featured articles: {len(set(featured))} (always visible)")
print(f" Regular articles: {len(set(regular))} unique articles")
if regular:
regular_ids = sorted(set(int(id) for id in regular))
print(f" Regular article IDs: {min(regular_ids)} to {max(regular_ids)}")
print(f" ℹ️ Note: Featured articles stay in DOM, only regular")
print(f" articles are replaced during virtual scroll")
finally:
server.stop()
async def compare_with_without_virtual_scroll():
"""
Comparison: Show the difference between crawling with and without virtual scroll.
"""
print("\n" + "="*60)
print("COMPARISON: With vs Without Virtual Scroll")
print("="*60)
server = TestServer()
port = await server.start()
try:
url = f"http://localhost:{port}/virtual_scroll_twitter_like.html"
# First, crawl WITHOUT virtual scroll
print("\n1️⃣ Crawling WITHOUT virtual scroll...")
async with AsyncWebCrawler() as crawler:
config_normal = CrawlerRunConfig(cache_mode=CacheMode.BYPASS)
result_normal = await crawler.arun(url=url, config=config_normal)
# Count items
import re
tweets_normal = len(set(re.findall(r'data-tweet-id="(\d+)"', result_normal.html)))
# Then, crawl WITH virtual scroll
print("2️⃣ Crawling WITH virtual scroll...")
virtual_config = VirtualScrollConfig(
container_selector="#timeline",
scroll_count=50,
scroll_by="container_height",
wait_after_scroll=0.2
)
config_virtual = CrawlerRunConfig(
virtual_scroll_config=virtual_config,
cache_mode=CacheMode.BYPASS
)
async with AsyncWebCrawler() as crawler:
result_virtual = await crawler.arun(url=url, config=config_virtual)
# Count items
tweets_virtual = len(set(re.findall(r'data-tweet-id="(\d+)"', result_virtual.html)))
# Compare results
print(f"\n📊 Comparison Results:")
print(f" Without virtual scroll: {tweets_normal} tweets (only initial visible)")
print(f" With virtual scroll: {tweets_virtual} tweets (all content captured)")
print(f" Improvement: {tweets_virtual / tweets_normal if tweets_normal > 0 else 'N/A':.1f}x more content!")
print(f"\n HTML size without: {len(result_normal.html):,} characters")
print(f" HTML size with: {len(result_virtual.html):,} characters")
finally:
server.stop()
if __name__ == "__main__":
print("""
╔════════════════════════════════════════════════════════════╗
║ Virtual Scroll Examples for Crawl4AI ║
╚════════════════════════════════════════════════════════════╝
These examples demonstrate different virtual scroll scenarios:
1. Twitter-like (content replaced) - Classic virtual scroll
2. Traditional append - Content accumulates
3. Instagram grid - Visual grid layout
4. Mixed behavior - Some content stays, some virtualizes
Starting examples...
""")
# Run all examples
asyncio.run(example_twitter_like_virtual_scroll())
asyncio.run(example_traditional_append_scroll())
asyncio.run(example_instagram_grid())
asyncio.run(example_mixed_content())
asyncio.run(compare_with_without_virtual_scroll())
print("\n✅ All examples completed!")
print("\nTIP: Set headless=False in BrowserConfig to watch the scrolling in action!")