图片来源网络

写在前面

最近在学习python,不得不说python真是好用,至少生成程序的速度快,语法也比较简单 ヾ(◍°∇°◍)ノ゙

感觉很强大,之前怎么就没有想到学一下这个呢,如果大学抢课的时候用python写一个简单的程序,就不用好几天守在电脑前了 (T▽T)

之前写了一篇博文《使用python+selenium爬小说》,用的是Web的UI自动化测试框架selenium,这次用框架Scrapy爬小说

目标网站

网站截图

爬小说思路

1、创建保存小说的文件夹。路径为D:\shortNovel\

2、获取当前页面的所有小说标题、小说链接

3、获取每篇小说的章节名称、章节链接

4、获取每个章节的内容,将内容保存到对应的txt文件中

5、判断小说列表页面是否有下一页,如果有,则转到下一页,然后进行2的步骤

注意点

执行代码过程中,发现一篇小说的章节是乱序的,这个和scrayp的异步请求有关系,为了保证小说的章节是有序的,在settings.py做如下的配置,设置请求的数量为1

# Configure maximum concurrent requests performed by Scrapy (default: 16)

CONCURRENT_REQUESTS = 1

编写代码

1、项目生成

使用命令scrapy startproject shortNovel生成项目shortNovel

F:\python>scrapy startproject shortNovel

New Scrapy project 'shortNovel', using template directory 'i:\\users\\dengdai68\\appdata\\local\\programs\\python\\python36\\lib\\site-packages\\scrapy\\templates\\project', created in:

F:\python\shortNovel

You can start your first spider with:

cd shortNovel

scrapy genspider example example.com

2、参考代码

在shortNovel的spider文件夹中,新建文件shortNovelSpider.py。内容如下

#coding=utf-8

import scrapy

from scrapy.http import Request

import os

class shortNovelSpider(scrapy.Spider):

page = 2

name = 'shortNovel'

allowed_domains = ['qidian.com']

dirPath = 'D:\\shortNovel\\'

path=dirPath.strip()

path=path.rstrip("\\")

if not os.path.exists(path):

os.makedirs(path)

novelTitle = []

novelLink = []

url = 'https://www.qidian.com/free/all'

def getPageNovel():

pass

def start_requests(self):

yield Request(self.url, self.parse_novels)

def parse_novels(self, response):

novelLink = []

novelTitle = response.xpath('//*[@id="free-channel-wrap"]/div/div/div[2]/div[2]/div/ul/li/div/h4/a/text()').extract()

novelLink = response.xpath('//*[@id="free-channel-wrap"]/div/div/div[2]/div[2]/div/ul/li/div/h4/a/@href').extract()

#print (novelTitle)

#print (novelLink)

#len(novelLink)

#获取小说列表

for i in range(len(novelLink)):

file = open(self.dirPath+novelTitle[i]+'.txt','a+')

file.write(novelTitle[i])

file.write('\n')

file.close()

novelLink[i] = 'https:'+novelLink[i]+'#Catalog'

yield Request(novelLink[i], callback=self.parse_novel)

#下一页

nextPageList = response.xpath('//*[@id="page-container"]/div/ul/li/a/text()').extract()

nextPageLink = response.xpath('//*[@id="page-container"]/div/ul/li/a/@href').extract()

print(nextPageList)

nextLink = ''

for i in range(len(nextPageList)):

if nextPageList[i] == str(self.page):

self.page = self.page + 1

nextLink = 'https:'+nextPageLink[i]

break

yield Request(nextLink, callback=self.parse_novels)

def parse_novel(self, response):

chaptersTitle = []

chaptersLink = []

reqs = []

chaptersTitle = response.xpath('//*[@id="j-catalogWrap"]/div[2]/div/ul/li/a/text()').extract()

chaptersLink = response.xpath('//*[@id="j-catalogWrap"]/div[2]/div/ul/li/a/@href').extract()

#print (chapters)

#获取每一章节

for link in reversed(chaptersLink):

link = 'https:'+link

yield Request(link, callback=self.parse_chapter)

def parse_chapter(self, response):

novelName = response.xpath('//*[@class="act"]/text()').extract()[0]

chapterName = response.xpath('//*[@class="j_chapterName"]/text()').extract()

chapterText = response.xpath('//*[@class="read-content j_readContent"]//p/text()').extract()

file = open(self.dirPath+novelName+'.txt','a+')

file.write(chapterName[0])

file.write('\n')

for itext in range(len(chapterText)):

file.write(chapterText[itext])

file.write('\n')

file.close()

运行结果

执行命令scrapy crawl shortNovel,结果如下

执行结果

写在最后

1、这次使用的是scrapy框架,运行效果不错,爬取的速度比使用selenium快很多

2、使用上面的代码时要注意自己的磁盘空间,因为执行到最后是把每一页的小说爬下来,如果只要第一页,可以注释了以下代码

#下一页

nextPageList = response.xpath('//*[@id="page-container"]/div/ul/li/a/text()').extract()

nextPageLink = response.xpath('//*[@id="page-container"]/div/ul/li/a/@href').extract()

print(nextPageList)

nextLink = ''

for i in range(len(nextPageList)):

if nextPageList[i] == str(self.page):

self.page = self.page + 1

nextLink = 'https:'+nextPageLink[i]

break

yield Request(nextLink, callback=self.parse_novels)

用python爬小说_使用python+Scrapy爬小说相关推荐

  1. python 时间序列预测_使用Python进行动手时间序列预测

    python 时间序列预测 Time series analysis is the endeavor of extracting meaningful summary and statistical ...

  2. python 概率分布模型_使用python的概率模型进行公司估值

    python 概率分布模型 Note from Towards Data Science's editors: While we allow independent authors to publis ...

  3. python爬收费小说_使用python+selenium爬小说

    图片来源网络 写在前面 最近在学习python,总想着自己动手弄一个简单一点的程序.于是想到了使用python和selenium爬小说 说起小说,想起了自己的一段"过往". 我以前 ...

  4. python爬取小说爬取_用python爬取笔趣阁小说

    原标题:用python爬取笔趣阁小说 首先打开笔趣阁网址,链接,搜索自己想要的小说. 在网站内单击右键,点击检查,会出现如下界面! 我们需要的章节信息就在我划的这块, 可以将每个标签点一下,它对应的内 ...

  5. python 实时数据推送_python scrapy 爬取金十数据并自动推送到微信

    一.背景 因业务需要获取风险经济事件并采取应对措施,但因为种种原因又疏忽于每天去查看财经日历,于是通过爬取金十数据网站并自动推送到微信查看. 二.目标实现 image 三.环境与工具 1.pychar ...

  6. 如何用python做考勤_【python爬虫教程 考勤】如何用Python实现一只小爬虫,爬取拉勾网...

    python爬虫入门教程全集 千锋官网上有一些是零基础入门学习的很不错 如何用Python实现一只小爬虫,爬取拉勾网 1.首先打开拉,并搜索"java",显示出职位信息就是我们的目 ...

  7. python房子代码_用python爬取租房网站信息的代码

    自己在刚学习python时写的,中途遇到很多问题,查了很多资料,下面就是我爬取租房信息的代码: 链家的房租网站 两个导入的包 1.requests 用来过去网页内容 2.BeautifulSoup i ...

  8. python输入数据爬取_利用 Python 爬取高德地图数据

    准备1.高德开放平台注册账户 https://lbs.amap.com/dev/index 验证手机号码.邮箱后进入开发者后台创建一个应用: 并为该应用添加 Key,服务平台选择 web 服务 申请完 ...

  9. python爬考研_用Python爬取了考研吧1000条帖子,原来他们都在讨论这些!

    写在前面 考研在即,想多了解考研er的想法,就是去找学长学姐或者去网上搜索,贴吧就是一个好地方.而借助强大的工具可以快速从网络鱼龙混杂的信息中得到有价值的信息.虽然网上有很多爬取百度贴吧的教程和例子, ...

最新文章

  1. 超越快手腾讯!度小满NLP模型登顶CLUE榜首
  2. ECCV2020最佳论文解读之递归全对场变换(RAFT)光流计算模型
  3. Sudo 漏洞隐患不断,macOS 也受牵连!
  4. JavaScript入门几个概念
  5. C++string类常用函数 c++中的string常用函数用法总结
  6. 领域驱动设计的简略设计步骤
  7. android layout 替换,LayoutInflater 后记--替换系统控件
  8. 树莓派linux i2c通信,树莓派与i2c设备的通信
  9. 有没有用逆向算法恢复马赛克的可能性?
  10. Visio使用遇到的问题
  11. JAVA输出希腊字母表
  12. 惯性系统常用坐标系_惯性坐标系与非惯性坐标系
  13. 4种“附近的人”实现方式
  14. walking机器人入门教程-工具-命令管理器
  15. Python 去除单色背景
  16. 设备巡检的执行的痛点及解决方案
  17. Java——重写hashCode()和euqals()方法
  18. 计算机类在职研究生考试有哪些专业,计算机在职研究生考试科目有哪些?
  19. 网络安全--主机探测教程
  20. Android点9图片被放大的问题

热门文章

  1. 微信 H5分享 下载jssdk文件
  2. 【Pytorch-从一团乱麻到入门】:4、模型效果评估指标:ROC-AUC、PR-AUC及可能遇到的问题(1)
  3. 关于更新app时出现解析包错误
  4. 【蓝桥杯】历届试题 对局匹配(贪心)
  5. 关于B2C电子商务顾客忠诚度影响因素的问卷调查
  6. CorelDrawX8安装时提示已安装另一个版本
  7. 高并发高流量网站架构
  8. 热电偶测温电路Multisim仿真及故障字典建立
  9. 4G来临,短视频社交分享应用或井喷
  10. 钉钉二次开发-组织机构同步 获取用户信息 单点登录接口