python 多线程实现网页自动截图

准备工作

安装selenium 2.48.0,一定不要安装最新版本的,最新版本不支持phantomjs。

用phantomjs是因为它是单文件版。下载地址:https://phantomjs.org/download.html

ip.txt的格式是 http://test.com(可根据需求自行更改)

完整代码

#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time    : 2020/8/3 16:10
# @Author  : oliver
# @File    : test.py
# @Software: PyCharm
import threading
import queue
import time
from selenium import webdriver
class get_screen(threading.Thread):
    def __init__(self,q):
        super(get_screen,self).__init__()
        self.q = q
    def run(self):
        while not self.q.empty():
            url = self.q.get()

            try:
                self.getScreen(url)

            except:
                pass

    def getScreen(self,url):

        brower = webdriver.PhantomJS(executable_path=r"E:phantomjs-2.1.1-windowsinphantomjs.exe")
        brower.get(url=url)
        brower.maximize_window()

        name = url.split("//")[1]
        picName = name + ".jpg"

        brower.save_screenshot(picName)
        brower.close()

def main():
    q = queue.Queue()
    thread_count =3
    threads =[]

    with open("ip.txt") as f:
        for line in f:
            line = line.strip("
")
            q.put(line)
    for i in range(thread_count):
        # print i
        threads.append(get_screen(q))
    for thread in threads:
        thread.start()

    for thread in threads:
        thread.join()



if __name__ == '__main__':
    start_time = time.time()
    main()
    print("截图完成,用时:%f" % (time.time() - start_time))
原文地址:https://www.cnblogs.com/oliver-yt/p/13435380.html