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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值