本文中我们将详细介绍使用Scrapy抓取数据并存入MongoDB数据库,首先给出我们需要抓取得数据:

抓取起点网得全部作品,网址为:https://www.qidian.com/all

关于Scrapy的下载与安装请移步上篇博客Scrapy简单案例

关于MongoDB的下载安装请移步博客MongoDB安装

下面直接给出相关代码;

(1) 数据封装类item.py

# -*- coding: utf-8 -*-# Define here the models for your scraped items
#
# See documentation in:
# https://doc.scrapy.org/en/latest/topics/items.htmlimport scrapyclass NovelItem(scrapy.Item):# define the fields for your item here like:# name = scrapy.Field()link = scrapy.Field()#URLcategory = scrapy.Field()bookname = scrapy.Field()author = scrapy.Field()content = scrapy.Field()

(2)爬虫主程序

# -*- coding: utf-8 -*-
import scrapyfrom novel.items import NovelItemclass SolveSpider(scrapy.Spider):name = "solve"allowed_domains = ["qidian.com"]start_urls = [];for x in range(1,5):#只有5页start_urls.append("https://www.qidian.com/all?orderId=&style=1&pageSize=20&siteid=1&pubflag=0&hiddenField=0&page=" + str(x))#start_urls = ["https://www.qidian.com/all?orderId=&style=1&pageSize=20&siteid=1&pubflag=0&hiddenField=0&page="]# page_index = ["1", "2", "3", "4", "5", "6", "7","8", "9", "10"]def parse(self, response):nolves = response.xpath('//ul[@class="all-img-list cf"]/li')for each in nolves:# print("***************************")item = NovelItem()part = each.xpath('./div[@class="book-mid-info"]')#print(part)item['bookname'] = part.xpath('./h4/a/text()').extract()[0]item['link'] = part.xpath('./h4/a/@href').extract()[0]item['author'] = part.xpath('./p[@class="author"]/a[@class="name"]/text()').extract()[0]item['category'] = part.xpath('./p[@class="author"]/a/text()').extract()[1]item['content'] = part.xpath('./p[@class="intro"]/text()').extract()[0]yield item

(3)管道pipeline.py

import  pymongoclass MongoDBPipeline(object):collection_name = 'scrapy_items'def __init__(self, mongo_uri, mongo_db):self.mongo_uri = mongo_uriself.mongo_db = mongo_db@classmethoddef from_crawler(cls, crawler):return cls(mongo_uri=crawler.settings.get('MONGO_URI'),mongo_db=crawler.settings.get('MONGO_DB'),)def open_spider(self, spider):self.client = pymongo.MongoClient(self.mongo_uri)self.db = self.client[self.mongo_db]self.collection = self.db["novel"]def close_spider(self, spider):self.client.close()def process_item(self, item, spider):self.collection.insert(dict(item))print("插入成功")return item

(4)配置文件


BOT_NAME = 'novel'SPIDER_MODULES = ['novel.spiders']
NEWSPIDER_MODULE = 'novel.spiders'
ITEM_PIPELINES = {'novel.pipelines.NovelPipeline':100,}MONGO_URI = "192.168.177.13"
MONGO_DB = "novels"
MONGO_COLLECTION = "novel"
# Crawl responsibly by identifying yourself (and your website) on the user-agent
#USER_AGENT = 'novel (+http://www.yourdomain.com)'# Obey robots.txt rules
ROBOTSTXT_OBEY = True
# 不验证SSL证书
DOWNLOAD_HANDLERS_BASE = {'file': 'scrapy.core.downloader.handlers.file.FileDownloadHandler','http': 'scrapy.core.downloader.handlers.http.HttpDownloadHandler','https': 'scrapy.core.downloader.handlers.http.HttpDownloadHandler','s3': 'scrapy.core.downloader.handlers.s3.S3DownloadHandler',
}

(5)查询结果

Scrapy爬取起点小说网数据导入MongoDB数据库相关推荐

  1. 起点小说免费看 Scrapy爬取起点小说网数据导入MongoDB数据

    本文中我们将详细介绍使用Scrapy抓取数据并存入MongoDB数据库,首先给出我们需要抓取得数据: 抓取起点网得全部作品,网址为:https://www.qidian.com/all 关于Scrap ...

  2. 【Python从零到壹】使用XPath解析数据爬取起点小说网数据

    我们已经可以从网上爬取数据了,现在我们来看看如何对数据解析 文章目录 1. xpath 的介绍 优点: 安装lxml库 XML的树形结构: 选取节点的表达式举例: 2. 爬取起点小说网 在浏览器中获取 ...

  3. scrapy爬取起点小说网

    闲来无事,在学习过程中练习用scrapy爬取起点小说名 工具:python3.6 操作系统:linux 浏览器:谷歌浏览器 创建项目 在黑屏终端创建一个项目:scrapy startproject Q ...

  4. Python爬虫期末作业 | 爬取起点小说网作者和书名,并以Excel形式存储

    使用Python爬虫技术爬取起点小说网作者及书名,并且以xlsx形式保存 前言 随着人工智能的不断发展,机器学习这门技术也越来越重要,很多人都开启了学习机器学习,本文就介绍了机器学习的基础内容. 一. ...

  5. 爬虫项目实战二:爬取起点小说网

    爬取起点小说网 目标 项目准备 网站分析 反爬分析 代码实现 效果显示 目标 爬取一本仙侠类的小说下载并保存为txt文件到本地.本例为"大周仙吏". 项目准备 软件:Pycharm ...

  6. java小说目录提取_完整Java爬取起点小说网小说目录以及对应链接

    完整Java爬取起点小说网小说目录以及对应链接 完整Java爬取起点小说网小说目录以及对应链接 (第一次使用markdown写,其中的排版很不好,望大家理解) ?? 因为最近有一个比赛的事情,故前期看 ...

  7. Scrapy爬取顶点小说网

    Scrapy爬取小说 爬取目标:顶点小说网 1.Scrapy的安装 pip install scrapy 2.Scrapy的介绍 创建项目 scrapy startproject xxx xxx项目名 ...

  8. 爬取起点小说网免费小说

    python 3.7 设置了0.5秒存入一个章节 所以有点慢 运行的时候在py文件的同级目录下创建目标的小说文件夹 在文件夹中写入小说章节 headers完全没有引用= =(主要是起点没有怎么反爬取) ...

  9. python爬网页、爬到前几个就不动了_python scrapy 爬取起点小说,爬虫停止在第四页不动了...

    如题,我在测试爬虫的时候,终端出现了如下代码: 2019-04-20 15:04:51 [scrapy.core.engine] DEBUG: Crawled (200) (referer: http ...

  10. Python的scrapy之爬取顶点小说网的所有小说

    闲来无事用Python的scrapy框架练练手,爬取顶点小说网的所有小说的详细信息. 看一下网页的构造: tr标签里面的 td 使我们所要爬取的信息 下面是我们要爬取的二级页面 小说的简介信息: 下面 ...

最新文章

  1. R语言基于机器学习算法进行特征筛选(Feature Selection)
  2. Linux初学(Linux命令行的使用)
  3. Verilog中case,casex,casez的区别
  4. 双系统引导修复与引导项删除
  5. java三年工作经验工资_工作三年还是只会增删改查,Java 程序员如何进阶?
  6. 提高程序员工作效率的11个技巧
  7. 统计文章中字母出现频率
  8. ssm数据库异常问题
  9. 深度优先搜索——First Step(洛谷 P3654)
  10. KendoUI--Grid api 出现的问题
  11. CEGUI的安装简要总结
  12. 解决办法:java.lang.UnsatisfiedLinkError ... Can't find dependent libraries
  13. [技术交流]一些技术网站推荐。金融IT。
  14. 增强火山图,试一下?
  15. Jetson Nano 从入门到实战(转载)(案例:Opencv配置、人脸检测、二维码检测)
  16. GWAS - plink提取染色体位置范围内的SNP位点
  17. 腾讯云轻量服务器蜂驰版测评及横向对比
  18. 修改global.func.php,DZ论坛核心代码分析-核心文件global.func.php篇
  19. 计算机为什么检测不到u盘启动项,BIOS如何设置U盘为电脑启动首选项_BIOS设置U盘启动项检测不到处理方法...
  20. 心里藏着小星星,生活才能亮晶晶

热门文章

  1. 电脑桌面数字时钟c语言,DesktopDigitalClock(桌面数字时钟)
  2. 汉仪字体安装后PPT找不到_字体不知道去哪下载?我教您
  3. linux上2048游戏程序,如何在Ubuntu中安装2048游戏
  4. 对股票进行可视化分析
  5. linux系统的手机刷机包,ubuntu系统修复工具-ubuntu手机操作系统刷机包v20.04 官方版 - 极光下载站...
  6. Objective-C 函数(方法)的定义和调用
  7. 主机前置耳机孔没声音
  8. 立创eda学习笔记二十五:绘制原理图的电气网络(绘制导线,使用节点)
  9. 通过TMS320F28335学习DSP的开发流程——28335的基础知识(1)
  10. Linux窗口字是倒着的,linux反撇号怎么打出来