首先我们需要几个包:requests, lxml, bs4, pymongo, redis
1. 创建爬虫对象,具有的几个行为:抓取页面,解析页面,抽取页面,储存页面
class Spider(object):
def __init__(self):
# 状态(是否工作)
self.status = SpiderStatus.IDLE
# 抓取页面
def fetch(self, current_url):
pass
# 解析页面
def parse(self, html_page):
pass
# 抽取页面
def extract(self, html_page):
pass
# 储存页面
def store(self, data_dict):
pass
2. 设置爬虫属性,没有在爬取和在爬取中,我们用一个类封装, @unique使里面元素独一无二,Enum和unique需要从 enum里面导入:
@unique
class SpiderStatus(Enum):
IDLE = 0
WORKING = 1
3. 重写多线程的类:
class SpiderThread(Thread):
def __init__(self, spider, tasks):
super().__init__(daemon=True)
self.spider = spider
self.tasks = tasks
def run(self):
while True:
pass
4. 现在爬虫的基本结构已经做完了,在main函数创建tasks, Queue需要从queue里面导入:
def main():
# list没有锁,所以使用Queue比较安全, task_queue=[]也可以使用,Queue 是先进先出结构, 即 FIFO
task_queue = Queue()
# 往队列放种子url, 即搜狐手机端的url
task_queue.put('http://m.sohu,com/')
# 指定起多少个线程
spider_threads = [SpiderThread(Spider(), task_queue) for _ in range(10)] for spider_thread in spider_threads:
spider_thread.start()
# 控制主线程不能停下,如果队列里有东西,任务不能停, 或者spider处于工作状态,也不能停
while task_queue.empty() or is_any_alive(spider_threads):
pass
print('Over')
4-1. 而 is_any_threads则是判断线程里是否有spider还活着,所以我们再写一个函数来封装一下:
def is_any_alive(spider_threads):
return any([spider_thread.spider.status == SpiderStatus.WORKING
for spider_thread in spider_threads])
5. 所有的结构已经全部写完,接下来就是可以填补爬虫部分的代码,在SpiderThread(Thread)里面,开始写爬虫运行 run 的方法,即线程起来后,要做的事情:
def run(self):
while True:
# 获取url
current_url = self.tasks_queue.get()
visited_urls.add(current_url)
# 把爬虫的status改成working
self.spider.status = SpiderStatus.WORKING
# 获取页面
html_page = self.spider.fetch(current_url)
# 判断页面是否为空
if html_page not in [None, '']: