-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathweb_scraping_utility.py
More file actions
63 lines (46 loc) · 1.54 KB
/
Copy pathweb_scraping_utility.py
File metadata and controls
63 lines (46 loc) · 1.54 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
import os, time
from selenium import webdriver
options = webdriver.ChromeOptions()
options.add_argument('--headless=new')
def test_headless(website: str, screenshot_path, screen_width: int = 1920, screen_height: int = 1080):
"""
Parameters
----------
website: str
The website to take a screenshot of
screenshot_path: str
The path to save the screenshot to
screen_width: int
The width of the screen to take the screenshot of
screen_height: int
The height of the screen to take the screenshot of
Returns
-------
None
Note
----
The screenshot will be saved as a .png file
This function is built just to test the headless option of selenium.
Example
-------
from toolbox import web_scraping_utility
import os
current_dir = os.getcwd()
screenshot_path = os.path.join(current_dir, 'test')
web_scraping_utility.test_headless('https://www.youtube.com', screenshot_path)
"""
options = webdriver.ChromeOptions()
options.add_argument('--headless')
# Initialize the Chrome webdriver and pass the options
driver = webdriver.Chrome(options=options)
driver.set_window_size(screen_width, screen_height)
driver.get(website)
# If path doesn't have a .png extension, add it
if not screenshot_path.endswith('.png'):
screenshot_path += '.png'
time.sleep(4)
# Take screenshot
driver.save_screenshot(screenshot_path)
driver.quit()
if __name__ == '__main__':
test_headless('https://www.youtube.com', 'test')