Python爬虫(四):爬取136书屋小说,并保存至本地文本文件中,单进程多进程对比效率(以三生三世十里桃花为例)
2017-12-12 00:03
826 查看
运行环境:
GitHub地址:https://github.com/IMWoolei/136Book_SaveNovel
通过修改136book小说网中具体小说的url来爬取不同小说的分章节批量下载
该代码以三生三世十里桃花为例(链接)
–>
运行效果展示
①
单进程保存小说章节
在下载保存的过程中发现,消耗的时间比较长。所以计划使用多进程来提高效率
②
多进程保存小说章节
运行过程中,在任务管理器中可以看到创建了
之前创建了200个进程后,直接电脑崩掉了
(●’◡’●)
Python3.6、
Pycharm2017.3、
Windows10 RS3、
requests2.18.4、
BeautifulSoup4.6.0
GitHub地址:https://github.com/IMWoolei/136Book_SaveNovel
简介
小说网址:http://www.136book.com/
通过修改136book小说网中具体小说的url来爬取不同小说的分章节批量下载
该代码以三生三世十里桃花为例(链接)
–>
http://www.136book.com/sanshengsanshimenglitaohua/
运行效果展示
① book136_singleprocess.py
单进程保存小说章节#!/usr/bin/env python # -*- coding: utf-8 -*- 4000 # @Author : Woolei # @File : book136_singleprocess.py import requests import time import os from bs4 import BeautifulSoup headers = { 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.84 Safari/537.36' } # 获取小说章节内容,并写入文本 def getChapterContent(each_chapter_dict): content_html = requests.get(each_chapter_dict['chapter_url'], headers=headers).text soup = BeautifulSoup(content_html, 'lxml') content_tag = soup.find('div', {'id': 'content'}) p_tag = content_tag.find_all('p') print('正在保存的章节 --> ' + each_chapter_dict['name']) for each in p_tag: paragraph = each.get_text().strip() with open(each_chapter_dict['name'] + r'.txt', 'a', encoding='utf8') as f: f.write(' ' + paragraph + '\n\n') f.close() # 获取小说各个章节的名字和url def getChapterInfo(novel_url): chapter_html = requests.get(novel_url, headers=headers).text soup = BeautifulSoup(chapter_html, 'lxml') chapter_list = soup.find_all('li') chapter_all_dict = {} for each in chapter_list: import re chapter_each = {} chapter_each['name'] = each.find('a').get_text() # 获取章节名字 chapter_each['chapter_url'] = each.find('a')['href'] # 获取章节url chapter_num = int(re.findall('\d+', each.get_text())[0]) # 提取章节序号 chapter_all_dict[chapter_num] = chapter_each # 记录到所有的章节的字典中保存 return chapter_all_dict if __name__ == '__main__': start = time.clock() # 记录程序运行起始时间 novel_url = 'http://www.136book.com/sanshengsanshimenglitaohua/' # 这里以三生三世十里桃花为例 novel_info = getChapterInfo(novel_url) # 获取小说章节记录信息 dir_name = '保存的小说路径' if not os.path.exists(dir_name): os.mkdir(dir_name) os.chdir(dir_name) # 切换到保存小说的目录 for each in novel_info: getChapterContent(novel_info[each]) # time.sleep(1) end = time.clock() # 记录程序结束的时间 print('保存小说结束,共保存了 %d 章,消耗时间:%f s' % (len(novel_info), (end - start)))
在下载保存的过程中发现,消耗的时间比较长。所以计划使用多进程来提高效率
② book136_multiprocess.py
多进程保存小说章节#!/usr/bin/env python # -*- coding: utf-8 -*- # @Author : Woolei # @File : book136_2.py import requests import time import os from bs4 import BeautifulSoup from multiprocessing import Pool url = 'http://www.136book.com/huaqiangu/ebxeeql/' headers = { 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.84 Safari/537.36' } # 获取小说章节内容,并写入文本 def getChapterContent(each_chapter_dict): content_html = requests.get(each_chapter_dict['chapter_url'], headers=headers).text soup = BeautifulSoup(content_html, 'lxml') content_tag = soup.find('div', {'id': 'content'}) p_tag = content_tag.find_all('p') print('正在保存的章节 --> ' + each_chapter_dict['name']) for each in p_tag: paragraph = each.get_text().strip() with open(each_chapter_dict['name'] + r'.txt', 'a', encoding='utf8') as f: f.write(' ' + paragraph + '\n\n') f.close() # 获取小说各个章节的名字和url def getChapterInfo(novel_url): chapter_html = requests.get(novel_url, headers=headers).text soup = BeautifulSoup(chapter_html, 'lxml') chapter_list = soup.find_all('li') chapter_all_dict = {} for each in chapter_list: import re chapter_each = {} chapter_each['name'] = each.find('a').get_text() # 获取章节名字 chapter_each['chapter_url'] = each.find('a')['href'] # 获取章节url chapter_num = int(re.findall('\d+', each.get_text())[0]) # 提取章节序号 chapter_all_dict[chapter_num] = chapter_each # 记录到所有的章节的字典中保存 return chapter_all_dict if __name__ == '__main__': start = time.clock() novel_url = 'http://www.136book.com/sanshengsanshimenglitaohua/' novel_info = getChapterInfo(novel_url) dir_name = '保存的小说路径' if not os.path.exists(dir_name): os.mkdir(dir_name) os.chdir(dir_name) pool = Pool(processes=10) # 创建10个进程 pool.map(getChapterContent, [novel_info[each] for each in novel_info]) pool.close() pool.join() end = time.clock() print('多进程保存小说结束,共保存了 %d 章,消耗时间:%f s' % (len(novel_info), (end - start)))
运行过程中,在任务管理器中可以看到创建了
10个子进程
(processes=10),可以创建多个进程来提高效率,但是不考虑电脑性能而创建过多的进程后会使电脑执行效率严重下降。
之前创建了200个进程后,直接电脑崩掉了
改进一下
可以将文件存储写成一个SaveFile类,调用方法可保存
.txt、
.md、
.html等格式文档,以方便查看。这就等一下再更新吧
(●’◡’●)
相关文章推荐
- Python编写一个简单的简单的爬虫-下载保存在本地
- [PYTHON]-用Scrapy爬虫遍历百度贴吧,本地保存文字版【PART 1】
- 通过网络图片小爬虫对比Python中单线程与多线(进)程的效率
- python爬虫数据保存到本地各种格式的方法
- Python 爬虫多线程爬取美女图片保存到本地
- Python3爬虫学习4:降爬取的信息保存到本地
- python 站点爬虫 下载在线盗墓笔记小说到本地的脚本
- 不务正业--用python爬虫抓取Konachan的图片并保存到本地文件
- python学习(二)爬虫——爬取网站小说并保存为txt文件(二)
- 人生苦短,我用Python--爬虫模拟登陆教务处并且保存数据到本地
- [PYTHON]-用Scrapy爬虫遍历百度贴吧,本地保存文字版【PART 2】
- Python 爬虫抓取美女图片保存到本地
- python 网站爬虫 下载在线盗墓笔记小说到本地的脚本
- Python爬虫获取图片并下载保存至本地
- [Python Scrapy爬虫] 二.翻页爬取农产品信息并保存本地
- python爬虫由浅入深9---定向爬取股票数据信息并保存至本地文件
- Python中单线程、多线程和多进程的效率对比实验
- python爬虫由浅入深1-从网页中爬取文件并保存至本地
- [Python]使用Scrapy爬虫框架简单爬取图片并保存本地
- Python使用爬虫抓取美女图片并保存到本地的方法【测试可用】