我们运用爬虫进行数据爬取的过程中,如果遇到海量的数据导致爬取时间过长无疑狠影响效率。这时,聪明的爬虫工程师就想到了一种爬取提高效率,缩短时间的方法——多线程爬虫。
我们列举一个案例——爬取腾讯招聘技术类前十页的数据。先po代码!
import requests
from urllib import parse
from bs4 import BeautifulSoup
import threading
import time
from queue import Queue
content_q = Queue #创建了一个response队列
flag =False #立一个flag,令他为false
class Thread_product(threading.Thread): #这里用了生产者和消费者的概念,将获取网页数据的类方法作为生产者,解析网页数据生成想要数据的类方法作为消费者
def __init__(self,i,q):
threading.Thread.__init__(self)
self.i=i
self.q=q
def run(self):
while True:
if self.q.empty(): #当页码队列为空时,结束传值
break
time