-
Notifications
You must be signed in to change notification settings - Fork 2
/
webshot.py
160 lines (125 loc) · 4.71 KB
/
webshot.py
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
#!/usr/bin/python
import time
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.chrome.options import Options
from datetime import datetime
import logging
import os, sys
urls = []
directory = datetime.today().strftime('%Y-%m-%d-%H:%M')
removeCookiebar = True
headlessChrome = True
readFromFile = True
screenWidth = 1280
if (len(sys.argv) > 1):
readFromFile = False
urls = [sys.argv[1]]
logger = logging.getLogger('webshot')
logging.basicConfig(filename='logs.txt', filemode='a', format='%(asctime)s,%(msecs)d %(name)s %(levelname)s %(message)s', datefmt='%H:%M:%S', level=logging.INFO)
def log(string, type='info'):
logger.info(string)
print(string)
def open_from_file(file='urls.txt'):
f = open(file, 'r')
lines = f.readlines()
f.close()
return lines
def start(urls = [], restart = False):
global removeCookiebar, headlessChrome, readFromFile, screenWidth
success = 0
failed = []
for url in urls:
url = url.strip()
filename = get_filename(url)
try:
log('Open in chrome: ' + url + ' (' + str(success + 1) + '/' + str(len(urls)) + ')')
driver = open_url_chrome(url)
try:
log('Create screenshot from ' + url)
if (check_access_denied(driver)):
log('Access denied page detected :(')
failed.append(url)
take_screenshot(driver, filename, screenWidth)
log('Screenshot created successfully. ' + filename)
success += 1
except Exception as e:
log(e)
log('Failed to take screenshot ' + url)
failed.append(url)
except Exception as e:
log(e)
log('Failed to open url ' + url)
failed.append(url)
log(str(success) + ' screenshots created successfully of ' + str(len(urls)) + '. Failed: ' + str(len(failed)))
# Try again without cookiebar remove and headless chrome
if (restart == False) and (len(failed) > 0):
removeCookiebar = False
log('Try again take screenshots from ' + str(len(failed)) + ' failed pages')
start(failed, True)
if (len(failed) > 0):
log('Failed pages: ' + ' '.join(failed))
# Generate filepath from url
def get_filename(url):
url = url.replace('https://', '')
url = url.replace('http://', '')
url = url.replace('www.', '')
url = url.replace('.', '_')
url = url.replace('/', '')
if os.path.isdir(directory) == False:
os.mkdir(directory);
return directory + '/' + url + '.png'
# Open url in chrome
def open_url_chrome(url):
options = Options()
if (headlessChrome == True):
options.add_argument('--headless')
options.add_argument('--start-maximized')
driver = webdriver.Chrome(options=options)
driver.maximize_window()
driver.get(url)
return driver
# Open url in firefox
def open_url_firefox(url):
driver = webdriver.Firefox()
driver.maximize_window()
driver.get(url)
time.sleep(10)
return driver
# Remove cookiebar and modals
def remove_cookiebar(driver):
driver.execute_script('try {var elements = document.body.querySelectorAll("*"); var removeWithIdClassName = ["cookie", "modal", "popup", "window"]; for (var i = 0; i < elements.length; i++) {var el = elements[i]; var st = getComputedStyle(el); var ci = ((el.id) ? el.id.toString().toLowerCase() : "") + ((el.className) ? el.className.toString().toLowerCase() : ""); for (var j = 0; j < removeWithIdClassName.length; j++) {if (ci.indexOf(removeWithIdClassName[j]) !== -1) {if (el) el.remove(); } } if ((el) && (st) && ((st.position === "fixed") || (st.position === "sticky")) && (st.bottom && parseInt(st.bottom) < 100)) {el.remove(); } } } catch (e) {}')
def take_screenshot(driver, filename, screen_width):
driver.set_window_size(screen_width, 800)
time.sleep(10)
page_height = calculate_page_height(driver)
log('Page size: ' + str(screen_width) + 'x' + str(page_height))
driver.set_window_size(screen_width, int(page_height))
driver.execute_script('window.scrollTo(0, document.body.scrollHeight)')
driver.execute_script('window.scrollTo(0, 0)')
time.sleep(10)
if (removeCookiebar == True):
remove_cookiebar(driver)
driver.save_screenshot(filename)
driver.quit()
def calculate_page_height(driver):
try:
body = driver.find_element_by_tag_name('body')
html = driver.find_element_by_tag_name('html')
body_height = int(body.size['height'])
html_height = int(html.size['height'])
scroll_height = int(driver.execute_script('return document.body.scrollHeight'))
return max(body_height, scroll_height, html_height, 800)
except Exception as e:
return 3000
def check_access_denied(driver):
text = driver.find_element_by_tag_name('body').text
if text:
text = text.lower()
return text.find('denied') != -1 or text.find('forbidden') != -1
return False
if (readFromFile == True):
urls = open_from_file('urls.txt')
log('Webshot started to create ' + str(len(urls)) + ' screenshot(s)')
start(urls)
log('Webshot finished successfully')