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
| import os import time from selenium import webdriver from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
URL = 'https://blog.95id.com/screenshot_web.html' PIC_PATH = os.path.join(os.path.expanduser('~'), 'Pictures/python/screenshot') PIC_NAME = 'screenshot.png'
def screenshot_web(url=URL, path=PIC_PATH, name=PIC_NAME): '''capture the whole web page :param url: the website url :type url: str :param path: the path for saving picture :type str :param name: the name of picture :type name: str ''' if not os.path.exists(path): os.makedirs(path)
phone_headers = { 'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8', 'Accept-Language': 'zh-CN,zh;q=0.8,en-US;q=0.5,en;q=0.3', 'User-Agent': 'Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.108 Mobile Safari/537.36', }
web_headers = { 'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8', 'Accept-Language': 'zh-CN,zh;q=0.8,en-US;q=0.5,en;q=0.3', 'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/53.0.2785.104 Safari/537.36 Core/1.53.4882.400 QQBrowser/9.7.13059.400', }
cap = DesiredCapabilities.PHANTOMJS.copy()
for key, value in phone_headers.items(): cap['phantomjs.page.customHeaders.{}'.format(key)] = value
browser = webdriver.PhantomJS(desired_capabilities=cap) browser.implicitly_wait(10) iphone_width = 414 iphone_height = 736 browser.set_window_size(iphone_width, iphone_height) browser.set_window_size(iphone_width*4,iphone_height) browser.execute_script("document.body.style.zoom='400%'")
browser.get(url) time.sleep(5) pic_path = os.path.join(os.path.join(path, name)) print(pic_path) if browser.save_screenshot(pic_path): print('Done!') else: print('Failed!') browser.close() if __name__ == '__main__': screenshot_web(url='https://mp.weixin.qq.com/s/EKAljB2iGkI-8VJkhQ_dBw',path='pic/')
|