这是windows下面的截图方法
依赖库如下所示:
pip install selenium==2.48.0
方法一:
代码如下所示:
import time from selenium import webdriver def jieTu(reqUrl): br = webdriver.PhantomJS(executable_path=r'D:\phantomjs-2.1.1-windows\bin\phantomjs') br.maximize_window() br.get(reqUrl) time.sleep(5) picName = reqUrl[reqUrl.rfind('/'):][1:].replace(".html", "") br.save_screenshot("./savehtml/picture/{}.png".format(picName)) return str(picName)+'.png' if __name__ == '__main__': url = "https://www.baidu.com" # pic_name = url[url.rfind('/'):][1:].replace(".html", ".png") # print(pic_name) jieTu(url)
注意:在这里,如果没有phantomjs.exe文件将会报错
phantomjs下载地址(根据自己电脑的系统下):
https://phantomjs.org/download.html
方法二:
代码如下所示:
import time from selenium import webdriver from selenium.webdriver.chrome.options import Options URL = 'https://www.baidu.com/' # 用selenium打开网页 # (首先要下载 Chrome webdriver, 或 Chrome webdriver) ch_options = Options() ch_options.add_argument("--disable-blink-features=AutomationControlled") # 在启动浏览器时加入配置 driver = webdriver.Chrome(executable_path=r"D:\chromedriver.exe", chrome_options=ch_options) driver.get(URL) print(driver.page_source) time.sleep(5) driver.save_screenshot("./timeline.png")
注意:在这里需要下载的是chromedriver(版本需要与自己的google浏览器版本对应,系统与电脑的系统相匹配)
驱动下载地址:
https://npm.taobao.org/mirrors/chromedriver/