-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathselenium_phantomjs.py
executable file
·52 lines (48 loc) · 1.68 KB
/
selenium_phantomjs.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
import time
from selenium import webdriver
from selenium.common.exceptions import TimeoutException
from selenium.webdriver import DesiredCapabilities
# from selenium.common.exceptions import TimeoutException
headers1 = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/66.0.3359.181 Safari/537.36'
}
desired_capabilities = DesiredCapabilities.PHANTOMJS.copy()
for key, value in headers1.items():
desired_capabilities['phantomjs.page.customHeaders.{}'.format(key)] = value
driver = webdriver.PhantomJS(desired_capabilities=desired_capabilities)
driver.set_window_size(1920, 1080)
driver.set_page_load_timeout(0.0000200)
driver.set_script_timeout(100)
url = 'https://cnblogs.com'
try:
driver.get(url)
driver.execute_script("""
(function () {
var y = 0;
var step = 100;
window.scroll(0, 0);
function f() {
if (y < document.body.scrollHeight) {
y += step;
window.scroll(0, y);
setTimeout(f, 100);
} else {
window.scroll(0, 0);
document.title += "scroll-done";
}
}
setTimeout(f, 1000);
})();
""")
for i in range(30):
if "scroll-done" in driver.title:
break
time.sleep(6)
print('selenium save png')
# except Exception as e:
# print(e)
except TimeoutException:
driver.execute_script('window.stop()')
print('超时,wiindow stop')
# driver.save_screenshot('1231.png')
# driver.quit()