参考: 静觅scrapy教程
爬取目标:顶点小说网 http://www.23us.com/

希望顶点小说网不要生气

首先来编写items.py

#-*- coding:utf-8 -*-
# Define here the models for your scraped items
#
# See documentation in:
# http://doc.scrapy.org/en/latest/topics/items.html
import scrapyclass BingdianItem(scrapy.Item):name = scrapy.Field()#小说名author = scrapy.Field()#作者novelurl = scrapy.Field()#小说地址serialstatus = scrapy.Field()#状态serialnumber = scrapy.Field()#连载字数category = scrapy.Field()#文章类别name_id = scrapy.Field()#小说编号class DcontentItem(scrapy.Item):id_name = scrapy.Field()            #小说编号chaptercontent = scrapy.Field()     #章节内容num = scrapy.Field()                #用于绑定章节顺序chapterurl = scrapy.Field()         #章节地址chaptername = scrapy.Field()        #章节名字

spider文件

#bingdian.py
#-*- coding:utf-8 -*-
import scrapy
# import re
# from bs4 import BeautifulSoup
# from scrapy.http import Response
from bingdian.items import BingdianItem ,DcontentItem
from bingdian.SQLitepipelines.sql import Sqlclass bingdian_spider(scrapy.Spider):name = 'bingdian'allowed_domains = ['23us.com']start_urls = ['http://www.23us.com/class/1_1.html','http://www.23wx.com/class/2_1.html','http://www.23wx.com/class/3_1.html','http://www.23wx.com/class/4_1.html','http://www.23wx.com/class/5_1.html','http://www.23wx.com/class/6_1.html','http://www.23wx.com/class/7_1.html','http://www.23wx.com/class/8_1.html','http://www.23wx.com/class/9_1.html','http://www.23wx.com/class/10_1.html']def parse(self,response):books = response.xpath('//dd/table/tr[@bgcolor="#FFFFFF"]')#/table/tbody/tr[@bgcolor="#FFFFFF"]print (books.extract())for book in books:name = book.xpath('.//td[1]/a[2]/text()').extract()[0]author = book.xpath('.//td[3]/text()').extract()[0]novelurl = book.xpath('.//td[1]/a[2]/@href').extract()[0]serialstatus = book.xpath('.//td[6]/text()').extract()[0]serialnumber = book.xpath('.//td[4]/text()').extract()[0]category = book.xpath('//dl/dt/h2/text()').re(u'(.+) - 文章列表')[0]jianjieurl = book.xpath('.//td[1]/a[1]/@href').extract()[0]item = BingdianItem()item['name'] =  nameitem['author'] = authoritem['novelurl'] = novelurlitem['serialstatus'] = serialstatusitem['serialnumber'] = serialnumberitem['category'] = categoryitem['name_id'] = jianjieurl.split('/')[-1]yield itemyield scrapy.Request(novelurl,callback = self.get_chapter,meta = {'name_id' : item['name_id']})next_page = response.xpath('//dd[@class="pages"]/div/a[12]/@href').extract()[0] #获取下一页地址if next_page:yield  scrapy.Request(next_page)#获取章节名def get_chapter(self,response):num = 0allurls = response.xpath('//tr')for trurls in allurls:tdurls = trurls.xpath('.//td[@class="L"]')for url in tdurls:num = num + 1chapterurl = response.url + url.xpath('.//a/@href').extract()[0]chaptername = url.xpath('.//a/text()').extract()[0]rets = Sql.select_chapter(chapterurl)if rets[0] == 1:print(u'章节已经存在了')passelse:yield scrapy.Request(url = chapterurl,callback = self.get_chaptercontent,meta={'num':num,'name_id':response.meta['name_id'],'chaptername':chaptername,'chapterurl':chapterurl})#获取章节内容def get_chaptercontent(self,response):item = DcontentItem()item['num'] = response.meta['num']item['id_name'] = response.meta['name_id']item['chaptername'] = response.meta['chaptername']item['chapterurl'] = response.meta['chapterurl']content = response.xpath('//dd[@id="contents"]/text()').extract()item['chaptercontent'] = '\n   '.join(content)return item

name:
定义spider名字的字符串(string)。spider的名字定义了Scrapy如何定位(并初始化)spider,所以其必须是唯一的。 不过您可以生成多个相同的spider实例(instance),这没有任何限制。 name是spider最重要的属性,而且是必须的。

allowed_domains:
可选。包含了spider允许爬取的域名(domain)列表(list)。 当 OffsiteMiddleware 启用时, 域名不在列表中的URL不会被跟进。

parse():
是spider的一个方法。Request()默认回调函数(可以通过传递callback=(函数名)修改回调函数,例如后面的get_chapter()函数)。被调用时,每个初始URL完成下载后生成的 Response 对象将会作为唯一的参数传递给该函数。 该方法负责解析返回的数据(response data),提取数据(生成item)以及生成需要进一步处理的URL的 Request 对象。

修改settings.py文件

ITEM_PIPELINES = {# 'bingdian.pipelines.BingdianPipeline': 300,'bingdian.SQLitepipelines.pipelines.BingdingPipeline' : 1 #启用自定义pipelines 1 是优先级程度(1-1000随意设置,数值越低,组件的优先级越高)
}
#pipelines.py
#-*- coding:utf-8 -*-
from .sql import Sql
from bingdian.items import BingdianItem,DcontentItemclass BingdingPipeline(object):def process_item(self,item,spider):if isinstance(item,BingdianItem):name_id = item['name_id']ret = Sql.select_name(name_id)if ret[0] == 1:print('已经存在了')passelse:xs_name = item['name']xs_author = item['author']category = item['category']Sql.insert_dd_name(xs_name,xs_author,category,name_id)print(u'开始存小说标题')if isinstance(item,DcontentItem):url = item['chapterurl']name_id = item['id_name']num_id = item['num']xs_chaptername = item['chaptername']xs_content = item['chaptercontent']Sql.insert_dd_chaptername(xs_chaptername,xs_content,name_id,num_id,url)print(u'%s 存储完毕') % xs_chapternamereturn item

sql操作文件

#-*- coding:utf-8 -*-
import sqlite3conn = sqlite3.connect('test.db')
cursor = conn.cursor()
#创建 dd_name 表
cursor.execute('DROP TABLE IF EXISTS dd_name')
cursor.execute('create table dd_name(xs_name VARCHAR (255) DEFAULT NULL ,xs_author VARCHAR (255),category VARCHAR (255),name_id VARCHAR (255))')
#创建 dd_chaptername 表
cursor.execute('DROP TABLE IF EXISTS dd_chaptername')
cursor.execute('''CREATE TABLE dd_chaptername(xs_chaptername VARCHAR(255) DEFAULT NULL ,xs_content TEXT,id_name INT(11) DEFAULT NULL, num_id INT(11) DEFAULT NULL ,url VARCHAR(255))''')
class Sql:#插入数据@classmethoddef insert_dd_name(cls,xs_name,xs_author,category,name_id):# sql = "insert into dd_name (xs_name,xs_author,category,name_id) values (%(xs_name)s , %(xs_author)s , %(category)s , %(name_id)s)"sql = "insert into dd_name (xs_name,xs_author,category,name_id) values ('%s','%s','%s','%s')" % (xs_name,xs_author,category,name_id)cursor.execute(sql)conn.commit()#查重@classmethoddef select_name(cls,name_id):sql = "SELECT EXISTS (select 1 from dd_name where name_id = '%s')" % name_idcursor.execute(sql)return cursor.fetchall()[0]@classmethoddef insert_dd_chaptername(cls,xs_chaptername,xs_content,id_name,num_id,url):sql = '''INSERT INTO dd_chaptername(xs_chaptername , xs_content , id_name ,num_id ,url) VALUES ('%s' ,'%s' ,%s ,%s ,'%s')''' % (xs_chaptername,xs_content,id_name,num_id,url)cursor.execute(sql)conn.commit()@classmethoddef select_chapter(cls,url):sql = "SELECT EXISTS (select 1 from dd_chaptername where url = '%s')" % urlcursor.execute(sql)return cursor.fetchall()[0]




scrapy实例 ----- 爬取小说相关推荐

  1. scrapy框架爬取小说

    首先创建相关项目文件,打开cmd输入以下命令: scrapy startproject 项目名称 接着切换到目录文件: cd 项目名称 定义要爬取的网站: scrapy genspider 爬虫名称 ...

  2. python爬取小说基本信息_Python爬虫零基础实例---爬取小说吧小说内容到本地

    Python爬虫实例--爬取百度贴吧小说 写在前面本篇文章是我在简书上写的第一篇技术文章,作为一个理科生,能把仅剩的一点文笔拿出来献丑已是不易,希望大家能在指教我的同时给予我一点点鼓励,谢谢. 一.介 ...

  3. Python爬虫之scrapy框架-爬取小说信息

    1.需求分析 我们要得到小说热销榜的数据,每部小说的提取内容为:小说名字.作者.类型.形式.然后将得到的数据存入CSV文件. 2.创建项目 创建一个scrapy爬虫项目方式如下: (1)在D盘下面创建 ...

  4. Scrapy 实例——爬取豆瓣图书排名top250

    1.安装 pip install E:\python\lxml-4.2.6-cp36-cp36m-win_amd64.whl pip install E:\python\Twisted-18.9.0- ...

  5. Scrapy实例:爬取中国天气网天气数据

    1.创建项目 在你存放项目的目录下,按shift+鼠标右键打开命令行,输入命令创建项目: PS F:\ScrapyProject> scrapy startproject weather # w ...

  6. Scrapy实例————爬取学堂在线合作院校页面内容

    目标 通过Scrapy爬取到合作院校的名称及该所院校在学堂在线开课的数量,将爬取到的数据保存到一个json文件中,例如:"清华大学,308",地址 http://www.xueta ...

  7. Scrapy爬虫框架,爬取小说网的所有小说

    Scrapy入门教程请看 目录 1.思路清理 2.创建爬虫项目 3. 爬虫架构构思 4.爬虫程序具体分析 5.效果展示 6.待优化的地方 1.思路清理 我们的目的是把某个小说网的所有小说给拿下,这就涉 ...

  8. python爬虫简单实例-爬取17K小说网小说

    什么是网络爬虫? 网络爬虫(Web Spider),又被称为网页蜘蛛,是一种按照一定的规则,自动地抓取网站信息的程序或者脚本. 爬虫流程 先由urllib的request打开Url得到网页html文档 ...

  9. python爬虫实例之——多线程爬取小说

    之前写过一篇爬取小说的博客,但是单线程爬取速度太慢了,之前爬取一部小说花了700多秒,1秒两章的速度有点让人难以接受. 所以弄了个多线程的爬虫. 这次的思路和之前的不一样,之前是一章一章的爬,每爬一章 ...

最新文章

  1. 陶哲轩之后,有人在这个猜想的证明之路上又前进了一步
  2. php mysql两个表合并_php – 我可以将两个MySQL查询合并为一个吗?
  3. mongodb与mysql命令对比 (前人笔记+自己添加)
  4. double click items in SBWP
  5. 根据application ID打印该application所包含的object
  6. 后端学习 - JDBC
  7. SGCheck:一个实验堆栈和全局数组溢出检测器
  8. 5.这就是搜索引擎:核心技术详解 --- 检索模型与搜索排序
  9. 从头开始学做 canvas 动画引擎
  10. TLPI UNIX linux系统编程手册源代码运行
  11. 小米5s安装xpose 上
  12. 无限循环滚动代码阿里巴巴国际站店铺装修代码底图滚动黑色半透明显示效果自定义内容装修代码全屏显示
  13. 英文构词词根表---不定式+过去式+过去分词
  14. 一阶系统单位阶跃响应的特点_一阶系统的单位阶跃响应.doc
  15. R实战 | 环状热图(circos)
  16. OTP一次性动态密码工具实现
  17. 【线性代数】4-2:投影(Porjections)
  18. ti芯片怎么成为一站式的代理
  19. golang工作中常用的一些库
  20. 2016全球大数据战略版图剖析(6):应用篇下

热门文章

  1. MATLAB绘制SOI指数
  2. 指标 | 指标波动归因
  3. Xshell用root用户连接Linux
  4. linux 格式化 4k对齐,硬盘WD10EARS在FreeNas的格式化方法,4k对齐的问题
  5. 服务器中毒怎么找出病毒源文件,电脑中毒后怎样查找出在哪个文件夹
  6. ubuntu中snap包的安装、更新删除与简单使用
  7. 数字信号处理——振动篇
  8. 日拱一卒——160个crackme之#5
  9. 他两次都没感动CCTV,却真正感动了中国
  10. 【Unity】加载时 Newtonsoft.Json 提示 Metadata file `Newtonsoft.Json.dll` does not contain valid metadata