前言
本文的文字及图片来源于网络,仅供学习、交流使用,不具有任何商业用途,如有问题请及时联系我们以作处理。

前文
01、python爬虫入门教程01:豆瓣Top电影爬取

基本开发环境
Python 3.6
Pycharm
相关模块的使用
requests
parsel
安装Python并添加到环境变量,pip安装需要的相关模块即可。

单章爬取

一、明确需求
爬取小说内容保存到本地

小说名字
小说章节名字
小说内容

# 第一章小说url地址
url = 'http://www.biquges.com/52_52642/25585323.html'
1
2
url = 'http://www.biquges.com/52_52642/25585323.html'
headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.138 Safari/537.36'
}
response = requests.get(url=url, headers=headers)
print(response.text)

请求网页返回的数据中出现了乱码,这就需要我们转码了。

加一行代码自动转码。

response.encoding = response.apparent_encoding

三、解析数据

根据css选择器可以直接提取小说标题以及小说内容。

def get_one_novel(html_url):# 调用请求网页数据函数response = get_response(html_url)# 转行成selector解析对象selector = parsel.Selector(response.text)# 获取小说标题title = selector.css('.bookname h1::text').get()# 获取小说内容 返回的是listcontent_list = selector.css('#content::text').getall()# ''.join(列表) 把列表转换成字符串content_str = ''.join(content_list)print(title, content_str)if __name__ == '__main__':url = 'http://www.biquges.com/52_52642/25585323.html'get_one_novel(url)![在这里插入图片描述](https://img-blog.csdnimg.cn/20210202135620489.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2NoaW5haGVyb2x0czIwMDg=,size_16,color_FFFFFF,t_70)四、保存数据(数据持久化)
使用常用的保[python基础教程](https://www.xin3721.com/eschool/pythonxin3721/)存方式: with opendef save(title, content):"""保存小说:param title: 小说章节标题:param content: 小说内容:return: """# 路径filename = f'{title}\\'# os 内置模块,自动创建文件夹if os.makedirs(filename):os.mkdir()# 一定要记得加后缀 .txt  mode 保存方式 a 是追加保存  encoding 保存编码with open(filename + title + '.txt', mode='a', encoding='utf-8') as f:# 写入标题f.write(title)# 换行f.write('\n')# 写入小说内容f.write(content)

保存一章小说,就这样写完了,如果想要保存整本小说呢?

整本小说爬虫
既然爬取单章小说知道怎么爬取了,那么只需要获取小说所有单章小说的url地址,就可以爬取全部小说内容了。

所有的单章的url地址都在 dd 标签当中,但是这个url地址是不完整的,所以爬取下来的时候,要拼接url地址。

def get_all_url(html_url):# 调用请求网页数据函数response = get_response(html_url)# 转行成selector解析对象selector = parsel.Selector(response.text)# 所有的url地址都在 a 标签里面的 href 属性中 dds = selector.css('#list dd a::attr(href)').getall()for dd in dds:novel_url = 'http://www.biquges.com' + ddprint(novel_url)if __name__ == '__main__':url = 'http://www.biquges.com/52_52642/index.html'get_all_url(url)

这样就获取了所有的小说章节url地址了。

爬取全本完整代码

import requests
import parsel
from tqdm import tqdmdef get_response(html_url):headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.138 Safari/537.36'}response = requests.get(url=html_url, headers=headers)response.encoding = response.apparent_encodingreturn responsedef save(novel_name, title, content):"""保存小说:param title: 小说章节标题:param content: 小说内容:return:"""filename = f'{novel_name}' + '.txt'# 一定要记得加后缀 .txt  mode 保存方式 a 是追加保存  encoding 保存编码with open(filename, mode='a', encoding='utf-8') as f:# 写入标题f.write(title)# 换行f.write('\n')# 写入小说内容f.write(content)def get_one_novel(name, novel_url):# 调用请求网页数据函数response = get_response(novel_url)# 转行成selector解析对象selector = parsel.Selector(response.text)# 获取小说标题title = selector.css('.bookname h1::text').get()# 获取小说内容 返回的是listcontent_list = selector.css('#content::text').getall()# ''.join(列表) 把列表转换成字符串content_str = ''.join(content_list)save(name, title, content_str)def get_all_url(html_url):# 调用请求网页数据函数response = get_response(html_url)# 转行成selector解析对象selector = parsel.Selector(response.text)# 所有的url地址都在 a 标签里面的 href 属性中dds = selector.css('#list dd a::attr(href)').getall()# 小说名字novel_name = selector.css('#info h1::text').get()for dd in tqdm(dds):novel_url = 'http://www.biquges.com' + ddget_one_novel(novel_name, novel_url)if __name__ == '__main__':novel_id = input('输入书名ID:')url = f'http://www.biquges.com/{novel_id}/index.html'get_all_url(url)

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/fei347795790/article/details/113055090

Python爬虫入门教程02:小说爬取相关推荐

  1. Python爬虫入门教程06:爬取数据后的词云图制作

    前言

  2. Python爬虫入门教程31:爬取猫咪交易网站数据并作数据分析

    前言

  3. Python爬虫入门教程32:爬取boss直聘招聘数据并做可视化展示

    前言

  4. Python爬虫入门教程27:爬取某电商平台数据内容并做数据可视化

    前言

  5. Python爬虫入门教程30:爬取拉勾网招聘数据信息

    前言

  6. Python爬虫--笔趣阁小说爬取

    Python爬虫–笔趣阁小说爬取 爬虫用到的插件 import requests from lxml import etree 小说目录页 以小说"我有百万技能点"为例,在笔趣阁搜 ...

  7. python爬虫入门练习:BeautifulSoup爬取猫眼电影TOP100排行榜,pandas保存本地excel文件

    传送门:[python爬虫入门练习]正则表达式爬取猫眼电影TOP100排行榜,openpyxl保存本地excel文件 对于上文使用的正则表达式匹配网页内容,的确是有些许麻烦,替换出现任何的差错都会导致 ...

  8. Python爬虫实战(02)—— 爬取诗词名句三国演义

    目录 前言 一.准备工作 二.爬取步骤 1. 引入库 2. 发送请求拿到页面 3.定位到章节URL 4.拼接URL拿到章节内容 5.存储各章节内容 完整代码 前言 这次爬虫任务是从诗词名句上爬取< ...

  9. Python爬虫学习教程,批量爬取下载抖音视频

    这篇文章主要为大家详细介绍了python批量爬取下载抖音视频,具有一定的参考价值,感兴趣的小伙伴们可以参考一下 项目源码展示: ''' 在学习过程中有什么不懂得可以加我的 python学习交流扣扣qu ...

最新文章

  1. 什么是信度分析(Reliability)?有哪些信度分析方法?那什么又是效度?
  2. Rsync 基本安装及同步(一)
  3. suse 11 oracle 10g,suse11+oracle10g安装
  4. java面向对象第一课,定义类,模拟人的行为:吃饭工作休息
  5. C/C++中MySQL环境配置教程
  6. 在浏览器设置里能看到cookie, 页面调试Application里看不到
  7. 《学习之道》第六章习惯的部分-反应程序
  8. 成功唯一的通道就是必须迷上你所做的事
  9. Intellij里面的几个异常处理方案
  10. 2018天梯赛第一次训练题解和ac代码
  11. 给IT新人的15点建议
  12. 关于GPIO的内部结构及编程步骤
  13. C语言源码实现俄罗斯方块
  14. 大家都买用阿里云服务器干什么用?
  15. Batch Normalization: Accelerating Deep Network Training by Reducing Internal Covariate Shift全文翻译
  16. !function(){}() 什么意思
  17. 七.面向对象编程(中)
  18. vscode中好用的git相关的插件
  19. 相对分子质量的计算怎样计算
  20. Matlab实现曲线拟合的最小二乘法

热门文章

  1. 初学Java—九九乘法表
  2. Python深度学习基予tensorflow(Numpy)
  3. 虚拟机启动出现“内部错误”解决方法
  4. ThinkPad E570重装系统后,外放无声音
  5. 利用chrome插件,向页面中注入js脚本
  6. mysql是怎样运行的 从根儿 百度云_MySQL 是怎样运行的:从根儿上理解 MySQL|完结|百度云下载...
  7. Unity Behavior Designer插件 任务模块
  8. Ubuntu安装教程2-Ubuntu桌面版安装
  9. 神是什么?神即道 道法自然 如来
  10. C#静态 xx相关学习