Selenium is a tool that allows the browser to automate a series of tasks, often used for automated testing. However, it can also be used to give Web pages. Currently, it supports four client languages for Java, C #, Ruby, and Python. If you use Python, you only need to enter "sudo easy_install selenium" on the command line and return to install Selenium's Python version of client support.
In Python, for example, we can use the following script to give specific pages (such as the home page of a script):
#-*-Coding:utf-8-*-## author:oldj <[email protected]># from selenium import webdriverimport time def Captur E (URL, save_fn= "Capture.png"): browser = webdriver. Firefox () # Get Local session of Firefox Browser.set_window_size (browser.get) # ( URL) # Load page Browser.execute_script ("" " (function () { var y = 0; var step = +; Window.Scroll (0, 0); function f () { if (Y < document.body.scrollHeight) { y + = step; Window.Scroll (0, y); SetTimeout (F, a); } else { window.scroll (0, 0); Document.title + = "Scroll-done"; } } SetTimeout (f, +); }) (); """) For I in xrange (+): if "Scroll-done" in Browser.title: break time.sleep (1) browser.save_ Screenshot (SAVE_FN) browser.close () if __name__ = = "__main__": Capture ("Http://www.jb51.net")
Note that, in the above code, I did not immediately after opening the page, but first executed a JavaScript script on the page, first drag the scroll bar to the bottom of the page, and then drag it back to the top before. The advantage of this is that if there are some deferred loading content below the page, it will generally be loaded after this operation.
Compared to browser plug-ins such as Pagesaver, the Selenium function is more powerful, for example, it can inject and execute a JS on the page, but also can simulate the mouse clicks and other behaviors, and can run multiple instances simultaneously (multiple threads simultaneously). In this case, using Selenium to give the page seems like a good choice.
Using Selenium to implement a Web page instance in Python