
掌握API網關認證:安全連接的可靠方法
深色版本
1pip install requests
下面是一個詳細的Python腳本示例,用于調用GrabzIt API捕獲網頁截圖。
python
深色版本
1import requests
2import base64
3
4def capture_screenshot(url, output_file, api_key, api_secret, format='png', full_page=True, width=1920, height=1080):
5 """
6 使用GrabzIt API捕獲網頁截圖。
7
8 :param url: 需要捕獲的網頁URL
9 :param output_file: 輸出文件名
10 :param api_key: GrabzIt API密鑰
11 :param api_secret: GrabzIt API密碼
12 :param format: 輸出文件格式,默認為PNG
13 :param full_page: 是否捕獲整個頁面,默認為True
14 :param width: 屏幕寬度,默認為1920
15 :param height: 屏幕高度,默認為1080
16 """
17
18 # 構建基本認證字符串
19 credentials = f'{api_key}:{api_secret}'
20 encoded_credentials = base64.b64encode(credentials.encode()).decode()
21
22 # 設置API請求參數
23 headers = {
24 'Authorization': f'Basic {encoded_credentials}',
25 'Content-Type': 'application/x-www-form-urlencoded',
26 }
27
28 data = {
29 'Url': url,
30 'Format': format, # 支持多種格式如pdf, jpg, docx等
31 'FullPage': str(full_page).lower(), # 捕獲整個頁面而非可見區域
32 'Width': str(width), # 屏幕寬度
33 'Height': str(height), # 屏幕高度
34 }
35
36 # 發送POST請求
37 response = requests.post('https://grabz.it/api/ScreenShot', headers=headers, data=data)
38
39 # 檢查響應狀態碼
40 if response.status_code == 200:
41 with open(output_file, 'wb') as file:
42 file.write(response.content)
43 print(f'Screenshot saved to {output_file}')
44 else:
45 print(f'Error capturing screenshot: {response.status_code} - {response.text}')
46
47# 示例
48url_to_capture = 'https://example.com'
49output_filename = 'screenshot.png'
50api_key = 'your_api_key_here'
51api_secret = 'your_api_secret_here'
52
53capture_screenshot(url_to_capture, output_filename, api_key, api_secret)
.py
文件,并運行它。your_api_key_here
和your_api_secret_here
為您的實際API密鑰和密碼。為了更有效地進行電子取證,可以將此腳本集成到定時任務或持續監控流程中,以定期捕獲指定網頁的截圖。例如,您可以使用Python的schedule
庫來實現定時任務。
python
深色版本
1import schedule
2import time
3
4def job():
5 capture_screenshot(url_to_capture, output_filename, api_key, api_secret)
6
7# 每天上午9點執行一次
8schedule.every().day.at("09:00").do(job)
9
10while True:
11 schedule.run_pending()
12 time.sleep(1)
width
和height
參數來創建更高分辨率的截圖。WaitForJavaScript
參數來實現。使用GrabzIt API進行網頁截圖是一種簡單而有效的方法,可以幫助您在電子取證過程中捕獲重要的網絡證據。通過自動化截圖,您可以節省大量的時間和資源,同時確保證據的完整性和時效性。