网页长截图

网页长截图

网页长截图

雄文十万,也挡不住管理方的删帖封号,本文就来说一下怎么通过技术的方式来保存网页内容

环境与依赖

  • python3
  • selenium库
  • PhantomJS

selenium

使用pip3安装即可:

1
pip3 install selenium

简单来说,selenium是一个可以操作浏览器或web渲染引擎的东西,方便自动化测试,比较明显的特征就是可以模拟浏览器中用户做的操作,比如点击、滚动、拖动等等,当然也能解析网页内容,所以这就使它经常被用来爬动态页面。

可以用selenium创建chromeFirefoxSafariPhantomJS浏览器实例,具体使用方法以后有时间整理一下。

PhantomJS

PhantomJS是一个无界面、可脚本编程的网页浏览器,与chrome--headless(使chrome无界面运行)参数运行的状态类似,为什么这里用PhantomJS?因为这个与Seleniumium库配合更容易截长图。

访问网址:http://phantomjs.org/download.html 进行下载对应平台的应用包。

解压后会看到bin目录,其下就是phantomjs命令行运行程序,将它扔到$PATH路径中,比如这样:

1
mv phantomjs /usr/local/bin/

重启终端,验证一下:

1
2
phantomjs -v
2.1.1

长截图

这里使用selenium库和phantomjs引擎实现长截图,selenium库其实就是封装了调用phantomjs的接口,非常方便!要实现某个网页的长截图,代码非常少:

screenshot_web.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
53
54
55
56
57
58
59
60
61
62
63
64
65
#!/usr/local/bin/python3
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',
}

#使用copy()防止修改原代码定义dict
cap = DesiredCapabilities.PHANTOMJS.copy()

for key, value in phone_headers.items():
cap['phantomjs.page.customHeaders.{}'.format(key)] = value

# 不载入图片,爬页面速度会快很多
#cap["phantomjs.page.settings.loadImages"] = False
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%'")
# driver.set_window_size(3000,800)
# driver.execute_script("document.body.style.zoom='250%'")

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/')

Comments

Your browser is out-of-date!

Update your browser to view this website correctly. Update my browser now

×