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

我们先开始使用单线程问题来看爬虫速度:(在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爬取可以同时快速的爬取。

  • 24
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

eqwaak0

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值