Repository navigation
Expand file tree
/
Copy pathactions.py
More file actions
279 lines (240 loc) · 10.7 KB
/
Copy pathactions.py
File metadata and controls
279 lines (240 loc) · 10.7 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
from crawl4ai import AsyncWebCrawler, CacheMode, CrawlResult
from crawl4ai.async_configs import CrawlerRunConfig
async def infinite_scroll(
crawler: AsyncWebCrawler,
markdown_generator,
url="https://www.scrapingcourse.com/infinite-scrolling",
session_id="hn_session",
):
# Wait until new commits appear
wait_condition = """js:() => {
const items = document.querySelectorAll('same4all');
return items?.length >= 0;
}"""
# Step 1: Load initial commits
next_config = CrawlerRunConfig(
wait_for=wait_condition,
session_id=session_id,
cache_mode=CacheMode.BYPASS,
markdown_generator=markdown_generator,
)
result: CrawlResult = await crawler.arun(
url=url, # "https://lnk.bio/akis_petretzikis"
config=next_config,
)
# selectors = auto_detect_selectors_with_dynamic_attributes(result.html)
# print("CSS Selectors:", selectors['css_selectors'])
selector = "div.pb-linkbox-inner.lnkbio-boxcolor.rounded.p-2.d-flex.flex-column.align-items-center.justify-content-center.ms-0.me-0"
pages_to_scroll = 3
items = result.cleaned_html.count("<img") * pages_to_scroll
print("Initial products loaded. Count:", result.cleaned_html.count("<img"), items)
# Wait until new commits appear
wait_for_more = f"""js:() => {{
const items = document.querySelectorAll('{selector}');
return items?.length > {items};
}}"""
next_config1 = CrawlerRunConfig(
session_id=session_id,
wait_for=wait_for_more,
wait_for_images=True, # Add this argument to ensure images are fully loaded
js_only=True, # We're continuing from the open tab
cache_mode=CacheMode.BYPASS,
scan_full_page=True, # Enables scrolling
markdown_generator=markdown_generator,
)
# use second time
result2 = await crawler.arun(url=url, config=next_config1)
# Step 3: Extract data from the page
# Access different media types
# images = result2.media["images"] # List of image details
# videos = result2.media["videos"] # List of video details
# audios = result2.media["audios"] # List of audio details
# print("video: ", videos, "images: ", images, "audios: ", audios)
# # Image with metadata
# for image in images:
# print(f"URL: {image['src']}")
# print(f"Alt text: {image['alt']}")
# print(f"Description: {image['desc']}")
# print(f"Relevance score: {image['score']}")
# # Process videos
# for video in videos:
# print(f"Video source: {video['src']}")
# print(f"Type: {video['type']}")
# print(f"Duration: {video.get('duration')}")
# print(f"Thumbnail: {video.get('poster')}")
# # Process audio
# for audio in audios:
# print(f"Audio source: {audio['src']}")
# print(f"Type: {audio['type']}")
# print(f"Duration: {audio.get('duration')}")
# Process content after each load
# print(f" items:",result2.metadata, result2.cleaned_html)
# print() # Print clean markdown content
# selectors = auto_detect_selectors_with_dynamic_attributes(result.html)
# print("CSS Selectors:", selectors['css_selectors'])
# print("XPath Selectors:", selectors['xpath_selectors'])
# print("After scroll+click, length:", len(result2.html))
# total_items = result2.html
return result2
async def load_more(
crawler: AsyncWebCrawler,
markdown_generator,
url="https://www.scrapingcourse.com/button-click",
session_id="hn_session",
):
# Wait until new commits appea
wait_condition = """js:() => {
const items = document.querySelectorAll('.product-item');
return items.length > 0;
}"""
# Step 1: Load initial commits
next_config = CrawlerRunConfig(
wait_for=wait_condition,
session_id=session_id,
cache_mode=CacheMode.BYPASS,
markdown_generator=markdown_generator,
# Not using js_only yet since it's our first load
)
# run_config = CrawlerRunConfig(
# # wait_for=f"css:{bg_images}",
# session_id=session_id,
# wait_for=f"js:{wait_condition}",
# # wait_for_images=True, # Add this argument to ensure images are fully loaded
# # process_iframes=True, # Process iframes
# # remove_overlay_elements=True, # Remove popups/modals that might block iframe
# # js_code=js_commands, # where js_code is the previous code content
# # magic=True, # Enable magic mode
# scan_full_page=True, # Enables scrolling
# scroll_delay=2, # Waits 200ms between scrolls (optional)
# # Mark that we do not re-navigate, but run JS in the same session:
# js_only=True,
# # cache_mode=CacheMode.BYPASS, # New way to handle cache
# # Only execute JS without reloading page
# # simulate_user=True, # Simulate human behavior
# # override_navigator=True, # Override navigator properties
# ) # Default crawl run configuration
result = await crawler.arun(url=url, config=next_config)
selector = "button#load-more-btn"
pages_to_load = 3
items = result.cleaned_html.count("product-image")
print("Initial products loaded. Count:", result.cleaned_html.count("product-image"))
# Step 2: For subsequent pages, we run JS to click 'Next Page' if it exists
js_next_page = f"""
window.scrollTo(0, document.body.scrollHeight);
const selector = '{selector}';
const button = document.querySelector(selector);
if(button) button.click();
"""
# Wait until new commits appear
wait_for_more = f"""js:() => {{
const items = document.querySelectorAll('.product-item');
return items.length > {items * pages_to_load};
}}"""
for page in range(1): # let's do 2 more "Next" pages
config_next = CrawlerRunConfig(
session_id=session_id,
js_code=[
js_next_page,
js_next_page,
js_next_page,
], # Execute JS to click 'Load more'
wait_for=wait_for_more,
js_only=True, # We're continuing from the open tab
cache_mode=CacheMode.BYPASS,
markdown_generator=markdown_generator,
# magic=True, # Enable magic mode
# scan_full_page=True, # Enables scrolling
# scroll_delay=2, # Waits 200ms between scrolls (optional)
)
result2 = await crawler.arun(url=url, config=config_next)
print(f"Page {page + 2} items count:", result2.cleaned_html)
# # Step 3: Extract data from the page
# # Access different media types
# images = result2.media["images"] # List of image details
# videos = result2.media["videos"] # List of video details
# audios = result2.media["audios"] # List of audio details
# # print("video: ", videos, "images: ", images, "audios: ", audios)
# # # Image with metadata
# for image in images:
# print(f"URL: {image['src']}")
# print(f"Alt text: {image['alt']}")
# print(f"Description: {image['desc']}")
# print(f"Relevance score: {image['score']}")
# # Process videos
# for video in videos:
# print(f"Video source: {video['src']}")
# print(f"Type: {video['type']}")
# print(f"Duration: {video.get('duration')}")
# print(f"Thumbnail: {video.get('poster')}")
# # Process audio
# for audio in audios:
# print(f"Audio source: {audio['src']}")
# print(f"Type: {audio['type']}")
# print(f"Duration: {audio.get('duration')}")
# print() # Print clean markdown content
# selectors = auto_detect_selectors_with_dynamic_attributes(result.html)
# print("CSS Selectors:", selectors['css_selectors'])
# print("XPath Selectors:", selectors['xpath_selectors'])
# print("After scroll+click, length:", len(result2.html))
# total_items = result2.html
return result2
async def basic_crawl(
crawler: AsyncWebCrawler,
markdown_generator,
url="https://www.scrapingcourse.com/button-click",
session_id="hn_session",
llm_strategy=None,
):
# wait_condition = """js:() => {
# const isButton = document.querySelector('button.fc-button.fc-confirm-choices.fc-primary-button');
# isButton?.click();
# return isButton !== null;
# }"""
# Step 1: Load initial commits
next_config = CrawlerRunConfig(
# wait_for=wait_condition,
session_id=session_id,
cache_mode=CacheMode.BYPASS,
magic=True, # Enable magic mode
markdown_generator=markdown_generator,
# wait_for_images=True, # Add this argument to ensure images are fully loaded
# process_iframes=True, # Extract iframe content
# remove_overlay_elements=True, # Remove popups/modals that might block iframe
# simulate_user=True, # Simulate human behavior
# override_navigator=True, # Override navigator properties
exclude_external_links=True,
exclude_social_media_links=True,
extraction_strategy=llm_strategy,
)
result2 = await crawler.arun(url=url, config=next_config)
# config_next1 = CrawlerRunConfig(
# session_id=session_id,
# js_only=True, # We're continuing from the open tab
# cache_mode=CacheMode.BYPASS,
# markdown_generator=markdown_generator, #
# # scan_full_page=True, # Enables scrolling
# # scroll_delay=2, # Waits 200ms between scrolls (optional)
# )
# await crawler.arun(
# url=url,
# config=config_next1
# )
# run_config = CrawlerRunConfig(
# # wait_for=f"css:{bg_images}",
# session_id=session_id,
# wait_for=f"js:{wait_condition}",
# # wait_for_images=True, # Add this argument to ensure images are fully loaded
# # process_iframes=True, # Process iframes
# # remove_overlay_elements=True, # Remove popups/modals that might block iframe
# # js_code=js_commands, # where js_code is the previous code content
# # magic=True, # Enable magic mode
# scan_full_page=True, # Enables scrolling
# scroll_delay=2, # Waits 200ms between scrolls (optional)
# # Mark that we do not re-navigate, but run JS in the same session:
# js_only=True,
# # cache_mode=CacheMode.BYPASS, # New way to handle cache
# # Only execute JS without reloading page
# # simulate_user=True, # Simulate human behavior
# # override_navigator=True, # Override navigator properties
# ) # Default crawl run configuration
return result2