所遇问题
- 1.许多网站采取的是滚动加载(懒加载)策略,想要截图页面加载网页全部内容。
实现步骤
driver_path = os.path.abspath(r'.\common\statics\chromedriver.exe')
service = Service(driver_path)
option = webdriver.ChromeOptions()
option.add_argument("--headless")
option.add_argument('--disable-gpu')
option.add_argument('--no-sandbox')
option.add_experimental_option("detach", True)
browser = webdriver.Chrome(service=service, options=option)
name = "截图保存的文件名"
url = "截图的网站URL"
try:
img_path = os.path.abspath(r'../') + "截图保存的路径"
browser.implicitly_wait(30)
browser.get(url)
js_height = "return document.body.clientHeight"
browser.get(url)
k = 1
height = browser.execute_script(js_height)
while True:
if k * 500 < height:
js_move = "window.scrollTo(0,{})".format(k * 500)
print(js_move)
browser.execute_script(js_move)
time.sleep(0.2)
height = browser.execute_script(js_height)
k += 1
else:
break
time.sleep(1)
width = browser.execute_script(
"return Math.max(document.body.scrollWidth, document.body.offsetWidth, "
"document.documentElement.clientWidth, document.documentElement.scrollWidth, "
"document.documentElement.offsetWidth);")
height = browser.execute_script(
"return Math.max(document.body.scrollHeight, document.body.offsetHeight, "
"document.documentElement.clientHeight, document.documentElement.scrollHeight, "
"document.documentElement.offsetHeight);")
browser.set_window_size(width + 100, height + 100)
time.sleep(1)
png_path = img_path + '/{}-2.png'.format(name)
browser.save_screenshot(png_path)
return True
except Exception as e:
print("截图的时候发生错误")
return False