提高爬虫速度问题(线程)

我们先开始使用单线程问题来看爬虫速度:(在wudi中有爬虫网站的地址)

import requests
import time

link_list=[]
with open('wudi.txt','r') as file:
    file_list=file.readlines()
    for eachone in file_list:
        link=eachone.split('\t')[1]
        link=link.replace('\n','')
        link_list.append(link)

    start=time.time()
    for eachone in link_list:
        try:
            r=requests.get(eachone)
            print(r.status_code,eachone)
        except Exception as e:
            print('Error:',e)
            
    end=time.time()

我们爬去5个时间是:100.428

如果是多线程的是话(有两种方法)

1.函数式:调用_thread模块里面的start_new_thread()函数产生的新线程。

2.类包装式:调用Threading库创建线程,从threading.Thread继承。

import _thread
import time

def print_time(threadName,delay):
    count=0
    while count<3:
        time.sleep(delay)
        count+=1
        print(threadName,time.ctime())

_thread.start_new_thread(print_time,("THread1",1))
_thread.start_new_thread(print_time,("THread2",2))
print('运行结果:')

我们可以看见代码的运行

在_thread里面的用法:

_thread.start_new_thread(function,args[,kwargs]).但是它相比于threading模块相对于还是太局限了。

threading的方法:

  1. run():用以表示线程活动的方法。
  2. start():启动线程活动。
  3. join([time]):等待线程的中止。
  4. isAlive():返回线程是否是活动的。
  5. getName():返回线程名。
  6. setName():设置线程名。

 

import threading
import time
class All(threading.Thread):
    def __init__(self,name,dealy):
        threading.Thread.__init__(self)
        self.name=name
        self.dealy=dealy
    def run(self):
        print('Starting'+self.name)
        print_time(self.name,self.dealy)
        print('Exiting'+self.name)

def print_time(threadName,delay):
    count=0
    while count<3:
        time.sleep(delay)
        print(threadName,time.ctime())
        count+=1
#创建线程
threads=[]
thread1=All('Thread1',1)
thread2=All('Thread2',2)
#开启线程
thread1.start()
thread2.start()

#添加线程到列表
thread.append(thread1)
thread.append(thread2)
#等待全部结束
for t in threads:
    t.join()
print('运行结束')

 threading能够有效地控制线程。

多线程爬虫:

import threading
import time
import requests

link_list = []
with open('wudi.txt', 'r') as file:
    file_list = file.readlines()
    for eachone in file_list:
        link = eachone.split('\t')[1]
        link = link.replace('\n', '')
        link_list.append(link)

start = time.time()
class myThread(threading.Thread):
    def __init__(self,name,link_range):
        threading.Thread.__init__(self)
        self.name=name
        self.link_range=link_range
    def run(self):
        print('starting'+self.name)
        crawler(self.name,self.link_range)
        print('exiting'+self.name)
def crawler(threadName,link_range):
        for i in range(link_range[0],link_range[1]+1):
            try:
                r = requests.get(link_list[i],timeout=20)
                print(threadName,r.status_code,link_list[i])
            except Exception as e:
                print(threadName,'Error:', e)

thread_list=[]
link_range_list=[{0,200},{201,400},{401,600},{601,800},{801,1000}]

for i in range(0,6):
    thread=myThread('Thread'+str(i),link_range_list[i-1])
    thread.start()
    thread_list.append(thread)

for thread in thread_list:
    thread.join()

end = time.time()
print('多线程爬取时间:',end-start)
print('over')

wudi的text是自己的的文件网页地址*3

多线程是我们把它们分为5份,先爬取完的退出,到最后还是单线程。

下次我们讲Queue爬取可以同时快速的爬取。

相关推荐

  1. 提升爬虫效率:多线,多进程,多协

    2024-04-01 20:52:04       29 阅读
  2. 线常见问题

    2024-04-01 20:52:04       22 阅读
  3. 线问题

    2024-04-01 20:52:04       17 阅读
  4. Qt提高-线池QThreadPool 详解

    2024-04-01 20:52:04       32 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-04-01 20:52:04       5 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-04-01 20:52:04       5 阅读
  3. 在Django里面运行非项目文件

    2024-04-01 20:52:04       4 阅读
  4. Python语言-面向对象

    2024-04-01 20:52:04       6 阅读

热门阅读

  1. 微信小程序(黑马优购:商品列表)

    2024-04-01 20:52:04       22 阅读
  2. 使用gradle离线编译ES 8.11

    2024-04-01 20:52:04       19 阅读
  3. Day35:学习尚上优选项目

    2024-04-01 20:52:04       20 阅读
  4. 说2个AI绘画自动生成器

    2024-04-01 20:52:04       18 阅读
  5. 机器视觉系统在工业零件检测中的应用

    2024-04-01 20:52:04       18 阅读
  6. HTML——1.简介、基础、元素

    2024-04-01 20:52:04       21 阅读
  7. C++与C语言

    2024-04-01 20:52:04       22 阅读
  8. LLM--打造Private GPT需要知道的一些概念及术语

    2024-04-01 20:52:04       26 阅读
  9. Linux 如何一键kill杀死某个被占用的端口

    2024-04-01 20:52:04       20 阅读
  10. c++ map

    2024-04-01 20:52:04       21 阅读
  11. vue3依赖注入解决根组件和多级组件件传值问题

    2024-04-01 20:52:04       18 阅读
  12. Stable Diffusion的界面参数详解

    2024-04-01 20:52:04       18 阅读