-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscraping_example.py
More file actions
52 lines (47 loc) · 1.49 KB
/
scraping_example.py
File metadata and controls
52 lines (47 loc) · 1.49 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
"""
Example: Demonstrating how to use the Scrapeless SDK scraping module
Filename: scraping_example.py
"""
import time
from scrapeless import Scrapeless, Logger
from scrapeless.types import ScrapingTaskRequest
request = ScrapingTaskRequest(
actor='scraper.google.search',
input={'q': 'nike site:www.nike.com'}
)
def google_search_example() -> None:
"""
Example for creating a scraping task and polling for the result synchronously.
"""
task = client.scraping.create_task(request=request)
if task['status'] == 200:
print('Task result:', task['data'])
return
while True:
time.sleep(1)
result = client.scraping.get_task_result(task['data']['taskId'])
if result['status'] == 200:
print('Polled result:', result['data'])
return
def google_search_by_scrape_example() -> None:
"""
Example for using the scraping scrape method synchronously.
"""
result = client.scraping.scrape(request=request)
print('Scrape result:', result)
def run_example() -> None:
"""
Main entry for running all scraping examples.
"""
try:
google_search_example()
except Exception as e:
Log.error(f'google_search_example error: {e}')
try:
google_search_by_scrape_example()
except Exception as e:
Log.error(f'google_search_by_scrape_example error: {e}')
print('Scraping example completed.')
Log = Logger().with_prefix('Scraping Example')
client = Scrapeless()
run_example()