1. 程式人生 > 實用技巧 >python 多執行緒實現網頁自動截圖

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-windows\bin\phantomjs.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("\n")
            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))