关键词:多条路径,并行结构,同时进行
正常情况下,爬虫程序都是有执行的先后顺序的,执行流程都在一条线上,即所谓的单线程爬虫。
相对应的,如果爬虫中的某部分程序可以并行执行,即在多条线上执行,则这种执行结构称为多线程结构(这里的多线程和Java等语言中的类似),对应的爬虫也称为多线程爬虫,。
代码解读如下:
#AB线程同时运行,时间上会减少
import threading
class A(threading.Thread):
def __init__(self):
threading.Thread.__init__(self) #初始化线程
def run(self):
for i in range(0,10):
print("我是线程A")
class B(threading.Thread):
def __init__(self):
threading.Thread.__init__(self) #初始化线程
def run(self):
for i in range(0,10):
print("我是线程B")
t1=A()
t1.start()
t2=B()
t2.start()
运行结果如下:
多线程爬虫优点:可以提高程序的运行效率,提高运行时间,尤其适合稍大型项目时。

正常爬虫程序多为单线程,执行有先后顺序。而多线程爬虫部分程序可并行执行,其结构为多线程结构。多线程爬虫能提高程序运行效率和时间,尤其适合稍大型项目。
398

被折叠的 条评论
为什么被折叠?



