1.继承自scrapy.Spider
2.独门秘笈
  CrawlSpider可以定义规则,再解析html内容的时候,可以根据链接规则提取出指定的链接,然后再向这些链接发送请求
  所以,如果有需要跟进链接的需求,意思就是爬取了网页之后,需要提取链接再次爬取,使用CrawlSpider是非常合适的
3.提取链接
  链接提取器,在这里就可以写规则提取指定链接
scrapy.linkextractors.LinkExtractor(
  allow = (),       # 正则表达式 提取符合正则的链接
  deny = (),       # (不用)正则表达式 不提取符合正则的链接
  allow_domains = (),   # (不用)允许的域名
  deny_domains = (),   # (不用)不允许的域名
  restrict_xpaths = (),   # xpath,提取符合xpath规则的链接
  restrict_css = ()     # 提取符合选择器规则的链接
)
4.模拟使用
  正则用法: links1 = LinkExtractor(allow=r'list_23_\d+\.html')
  xpath用法: links2 = LinkExtractor(restrict_xpaths=r'//div[@class="x"]')
  css用法:    links3 = LinkExtractor(restrict_css='.x')
5.提取连接
  link.extract_links(response)

对当前网页链接提取

导入链接提取器

使用正则语法,比较多

\b  表数字

\b+  一到多个数字

\.   转义点号

查看提取的链接

使用xpath语法

查看提取的链接

6.注意事项
  【注1】callback只能写函数名字符串, callback='parse_item'
  【注2】在基本的spider中,如果重新发送请求,那里的callback写的是 callback=self.parse_item 【注‐‐稍后看】follow=true 是否跟进 就是按照提取连接规则进行提取
运行原理:
读书网数据入库 
  1.创建项目:scrapy startproject dushuproject
  2.跳转到spiders路径 cd\dushuproject\dushuproject\spiders
  3.创建爬虫类:scrapy genspider ‐t crawl read www.dushu.com
  4.items
  5.spiders
  6.settings
  7.pipelines
  数据保存到本地
  数据保存到mysql数据库 

创建项目
> scrapy startproject scrapy_redbook_101
 
cd到spiders目录下,创建爬虫文件
spiders> scrapy genspider ‐t crawl read https://www.dushu.com/book/1188.html

items定义爬取的数据结构类

name名字、src图片

items数据结构类的导包

运行

开启管道

pipelines管道功能

运行

经过计算观察,缺失第一页的数据

运行

这次,没有问题,13页的所有数据


入库操作

链接mysql数据库

创建spider01数据库

使用spider01数据库

创建book表

查询book表内容

查询虚拟机ip

settings配置,链接使用数据库

开启数据库插入管道

pipelines数据库插入功能实现

  1. # Define your item pipelines here
  2. #
  3. # Don't forget to add your pipeline to the ITEM_PIPELINES setting
  4. # See: https://docs.scrapy.org/en/latest/topics/item-pipeline.html
  5.  
  6. # useful for handling different item types with a single interface
  7. from itemadapter import ItemAdapter
  8.  
  9. class ScrapyReadbook101Pipeline:
  10.  
  11. def open_spider(self,spider):
  12. self.fp = open('book.json','w',encoding='utf-8')
  13.  
  14. def process_item(self, item, spider):
  15. self.fp.write(str(item))
  16. return item
  17.  
  18. def close_spider(self,spider):
  19. self.fp.close()
  20.  
  21. # 加载settings文件,数据库参数
  22. from scrapy.utils.project import get_project_settings
    # 导入pyymsql
  23. import pymysql
  24.  
  25. # 创建mysql插入管道
  26. class MysqlPipeline:

  27.   # 获取数据库链接参数
  28. def open_spider(self,spider):
  29. settings = get_project_settings()
  30. self.host = settings['DB_HOST']
  31. self.port =settings['DB_PORT']
  32. self.user =settings['DB_USER']
  33. self.password =settings['DB_PASSWROD']
  34. self.name =settings['DB_NAME']
  35. self.charset =settings['DB_CHARSET']
  36.      # 链接
  37. self.connect()

  38. # 链接数据库函数实现,获取cursor对象
  39. def connect(self):
  40. self.conn = pymysql.connect(
  41. host=self.host,
  42. port=self.port,
  43. user=self.user,
  44. password=self.password,
  45. db=self.name,
  46. charset=self.charset
  47. )
  48.      # 创建执行mysql语句对象
  49. self.cursor = self.conn.cursor()
  50.  
  51.   # 操作数据库函数
  52. def process_item(self, item, spider):
  53.      # 插入操作
  54. sql = 'insert into book(name,src) values("{}","{}")'.format(item['name'],item['src'])
  55. # 执行sql语句
  56. self.cursor.execute(sql)
  57. # 提交
  58. self.conn.commit()
  59.  
  60. return item
  61.  
  62.   #关闭插入,关闭链接
  63. def close_spider(self,spider):
  64. self.cursor.close()
  65. self.conn.close()

运行

虚拟机中,查询表中数据

以上是13页数据的爬取


follow=true 跟进 按照提取连接规则进行提取

运行

虚拟机中,查询表中数据


项目文件夹

read.py爬虫核心文件

  1. import scrapy
  2. from scrapy.linkextractors import LinkExtractor
  3. from scrapy.spiders import CrawlSpider, Rule
  4.  
  5. from scrapy_readbook_101.items import ScrapyReadbook101Item
  6.  
  7. class ReadSpider(CrawlSpider):
  8. name = 'read'
  9. allowed_domains = ['www.dushu.com']
  10. start_urls = ['https://www.dushu.com/book/1188_1.html']
  11.  
  12. rules = (
  13. Rule(LinkExtractor(allow=r'/book/1188_\d+.html'),
  14. callback='parse_item',
  15. follow=True),
  16. )
  17.  
  18. def parse_item(self, response):
  19.  
  20. img_list = response.xpath('//div[@class="bookslist"]//img')
  21.  
  22. for img in img_list:
  23. name = img.xpath('./@data-original').extract_first()
  24. src = img.xpath('./@alt').extract_first()
  25.  
  26. book = ScrapyReadbook101Item(name=name,src=src)
  27. yield book

items.py自定义数据结构类

  1. # Define here the models for your scraped items
  2. #
  3. # See documentation in:
  4. # https://docs.scrapy.org/en/latest/topics/items.html
  5.  
  6. import scrapy
  7.  
  8. class ScrapyReadbook101Item(scrapy.Item):
  9. # define the fields for your item here like:
  10. # name = scrapy.Field()
  11. name = scrapy.Field()
  12. src = scrapy.Field()

settings.py参数配置文件

  1. # Scrapy settings for scrapy_readbook_101 project
  2. #
  3. # For simplicity, this file contains only settings considered important or
  4. # commonly used. You can find more settings consulting the documentation:
  5. #
  6. # https://docs.scrapy.org/en/latest/topics/settings.html
  7. # https://docs.scrapy.org/en/latest/topics/downloader-middleware.html
  8. # https://docs.scrapy.org/en/latest/topics/spider-middleware.html
  9.  
  10. BOT_NAME = 'scrapy_readbook_101'
  11.  
  12. SPIDER_MODULES = ['scrapy_readbook_101.spiders']
  13. NEWSPIDER_MODULE = 'scrapy_readbook_101.spiders'
  14.  
  15. # Crawl responsibly by identifying yourself (and your website) on the user-agent
  16. #USER_AGENT = 'scrapy_readbook_101 (+http://www.yourdomain.com)'
  17.  
  18. # Obey robots.txt rules
  19. ROBOTSTXT_OBEY = True
  20.  
  21. # Configure maximum concurrent requests performed by Scrapy (default: 16)
  22. #CONCURRENT_REQUESTS = 32
  23.  
  24. # Configure a delay for requests for the same website (default: 0)
  25. # See https://docs.scrapy.org/en/latest/topics/settings.html#download-delay
  26. # See also autothrottle settings and docs
  27. #DOWNLOAD_DELAY = 3
  28. # The download delay setting will honor only one of:
  29. #CONCURRENT_REQUESTS_PER_DOMAIN = 16
  30. #CONCURRENT_REQUESTS_PER_IP = 16
  31.  
  32. # Disable cookies (enabled by default)
  33. #COOKIES_ENABLED = False
  34.  
  35. # Disable Telnet Console (enabled by default)
  36. #TELNETCONSOLE_ENABLED = False
  37.  
  38. # Override the default request headers:
  39. #DEFAULT_REQUEST_HEADERS = {
  40. # 'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
  41. # 'Accept-Language': 'en',
  42. #}
  43.  
  44. # Enable or disable spider middlewares
  45. # See https://docs.scrapy.org/en/latest/topics/spider-middleware.html
  46. #SPIDER_MIDDLEWARES = {
  47. # 'scrapy_readbook_101.middlewares.ScrapyReadbook101SpiderMiddleware': 543,
  48. #}
  49.  
  50. # Enable or disable downloader middlewares
  51. # See https://docs.scrapy.org/en/latest/topics/downloader-middleware.html
  52. #DOWNLOADER_MIDDLEWARES = {
  53. # 'scrapy_readbook_101.middlewares.ScrapyReadbook101DownloaderMiddleware': 543,
  54. #}
  55.  
  56. # Enable or disable extensions
  57. # See https://docs.scrapy.org/en/latest/topics/extensions.html
  58. #EXTENSIONS = {
  59. # 'scrapy.extensions.telnet.TelnetConsole': None,
  60. #}
  61.  
  62. # 参数中一个端口号 一个是字符集 都要注意
  63. DB_HOST = '192.168.231.130'
  64. # 端口号是一个整数
  65. DB_PORT = 3306
  66. DB_USER = 'root'
  67. DB_PASSWROD = '1234'
  68. DB_NAME = 'spider01'
  69. # utf-8的杠不允许写
  70. DB_CHARSET = 'utf8'
  71.  
  72. # Configure item pipelines
  73. # See https://docs.scrapy.org/en/latest/topics/item-pipeline.html
  74. ITEM_PIPELINES = {
  75. 'scrapy_readbook_101.pipelines.ScrapyReadbook101Pipeline': 300,
  76. # MysqlPipeline
  77. 'scrapy_readbook_101.pipelines.MysqlPipeline':301
  78. }
  79.  
  80. # Enable and configure the AutoThrottle extension (disabled by default)
  81. # See https://docs.scrapy.org/en/latest/topics/autothrottle.html
  82. #AUTOTHROTTLE_ENABLED = True
  83. # The initial download delay
  84. #AUTOTHROTTLE_START_DELAY = 5
  85. # The maximum download delay to be set in case of high latencies
  86. #AUTOTHROTTLE_MAX_DELAY = 60
  87. # The average number of requests Scrapy should be sending in parallel to
  88. # each remote server
  89. #AUTOTHROTTLE_TARGET_CONCURRENCY = 1.0
  90. # Enable showing throttling stats for every response received:
  91. #AUTOTHROTTLE_DEBUG = False
  92.  
  93. # Enable and configure HTTP caching (disabled by default)
  94. # See https://docs.scrapy.org/en/latest/topics/downloader-middleware.html#httpcache-middleware-settings
  95. #HTTPCACHE_ENABLED = True
  96. #HTTPCACHE_EXPIRATION_SECS = 0
  97. #HTTPCACHE_DIR = 'httpcache'
  98. #HTTPCACHE_IGNORE_HTTP_CODES = []
  99. #HTTPCACHE_STORAGE = 'scrapy.extensions.httpcache.FilesystemCacheStorage'

pipelines.py功能核心功能

  1. # Define your item pipelines here
  2. #
  3. # Don't forget to add your pipeline to the ITEM_PIPELINES setting
  4. # See: https://docs.scrapy.org/en/latest/topics/item-pipeline.html
  5.  
  6. # useful for handling different item types with a single interface
  7. from itemadapter import ItemAdapter
  8.  
  9. class ScrapyReadbook101Pipeline:
  10.  
  11. def open_spider(self,spider):
  12. self.fp = open('book.json','w',encoding='utf-8')
  13.  
  14. def process_item(self, item, spider):
  15. self.fp.write(str(item))
  16. return item
  17.  
  18. def close_spider(self,spider):
  19. self.fp.close()
  20.  
  21. # 加载settings文件
  22. from scrapy.utils.project import get_project_settings
  23. import pymysql
  24.  
  25. class MysqlPipeline:
  26.  
  27. def open_spider(self,spider):
  28. settings = get_project_settings()
  29. self.host = settings['DB_HOST']
  30. self.port =settings['DB_PORT']
  31. self.user =settings['DB_USER']
  32. self.password =settings['DB_PASSWROD']
  33. self.name =settings['DB_NAME']
  34. self.charset =settings['DB_CHARSET']
  35.  
  36. self.connect()
  37.  
  38. def connect(self):
  39. self.conn = pymysql.connect(
  40. host=self.host,
  41. port=self.port,
  42. user=self.user,
  43. password=self.password,
  44. db=self.name,
  45. charset=self.charset
  46. )
  47.  
  48. self.cursor = self.conn.cursor()
  49.  
  50. def process_item(self, item, spider):
  51.  
  52. sql = 'insert into book(name,src) values("{}","{}")'.format(item['name'],item['src'])
  53. # 执行sql语句
  54. self.cursor.execute(sql)
  55. # 提交
  56. self.conn.commit()
  57.  
  58. return item
  59.  
  60. def close_spider(self,spider):
  61. self.cursor.close()
  62. self.conn.close()

CrawlSpider_获取图片名称地址,及入库的更多相关文章

  1. ResDrawableImgUtil【根据图片名称获取resID值或者Bitmap对象】

    版权声明:本文为HaiyuKing原创文章,转载请注明出处! 前言 根据图片名称获取项目的res/drawable-xxdhpi中相应资源的ID值以及bitmap值的封装类. 效果图 代码分析 根据图 ...

  2. java SpringWeb 接收安卓android传来的图片集合及其他信息入库存储

    公司是做APP的,进公司一年了还是第一次做安卓的接口 安卓是使用OkGo.post("").addFileParams("key",File); 通过这种方式传 ...

  3. angular上传获取图片的directive指令

    在AngularJS中,操作DOM一般在指令中完成,那么指令是如何实现的呢?指令的作用是把我们自定义的语义化标签替换成浏览器能够认识的HTML标签 一般的事件监听是在对静态的dom绑定事件,而如果在指 ...

  4. 阿里云使用js 实现OSS图片上传、获取OSS图片列表、获取图片外网访问地址(读写权限私有、读写权限公共);

    详情请参考:https://help.aliyun.com/document_detail/32069.html?spm=a2c4g.11186623.6.763.ZgC59a 或者https://h ...

  5. 如何获取Flickr图片链接地址作为外链图片

    Flickr,雅虎旗下图片分享网站.为一家提供免费及付费数位照片储存.分享方案之线上服务,也提供网络社群服务的平台.其重要特点就是基于社会网络的人际关系的拓展与内容的组织.这个网站的功能之强大,已超出 ...

  6. Android 获取手机Mac地址,手机名称

    /** * 获取手机mac地址<br/> * 错误返回12个0 */ public static String getMacAddress(Context context) { // 获取 ...

  7. Android BLE与终端通信(一)——Android Bluetooth基础API以及简单使用获取本地蓝牙名称地址

    Android BLE与终端通信(一)--Android Bluetooth基础API以及简单使用获取本地蓝牙名称地址 Hello,工作需要,也必须开始向BLE方向学习了,公司的核心技术就是BLE终端 ...

  8. 根据图片url地址获取图片的宽高

    /** * 根据img获取图片的宽高 * @param img 图片地址 * @return 图片的对象,对象中图片的真实宽高 */ public BufferedImage getBufferedI ...

  9. 图片url地址的生成获取方法

    在写博客插入图片时,许多时候需要提供图片的url地址.作为菜鸡的我,自然是一脸懵逼.那么什么是所谓的url地址呢?又该如何获取图片的url地址呢? 首先来看一下度娘对url地址的解释:url是统一资源 ...

随机推荐

  1. 三、mybatis多表关联查询和分布查询

    前言 mybatis多表关联查询和懒查询,这篇文章通过一对一和一对多的实例来展示多表查询.不过需要掌握数据输出的这方面的知识.之前整理过了mybatis入门案例和mybatis数据输出,多表查询是在前 ...

  2. Springboot实现VNC的反向代理

    背景 ​ 用户需要通过前端HTML页面的noVNC(noVNC是什么?)客户端连接底层VNC Server服务端,为了防止VNC Server的IP暴露,因此需要做一层代理.正常情况下使用Nginx. ...

  3. Java-爬虫-小项目

    爬取数据:(获取请求返回的页面信息,筛选出我们想要的数据就可以了!) 项目地址:https://gitee.com/zwtgit/java-reptile 导入依赖,写一个工具类 <depend ...

  4. Java中的基本类型和包装类

    Java中基本数据类型与包装类型有   基本类型     包装器类型   boolean Boolean char Character int Integer byte Byte short Shor ...

  5. final和static的区别

    static作用于成员变量用来表示只保存一份副本 final的作用是用来保证变量不可变.下面代码验证一下 public class FinalTest { public static void mai ...

  6. python的参数传递是值传递还是引用传递?都不是!

    [写在前面] 参考文章: https://www.cnblogs.com/spring-haru/p/9320493.html[偏理论,对值传递和引用传递作了总结] https://www.cnblo ...

  7. 【Spring】重新认识 IoC

    前言 IoC (Inversion of control) 并不是Spring特有的概念. IoC 维基百科的解释: In software engineering, inversion of con ...

  8. 【UE4】GAMES101 图形学作业3:Blinn-Phong 模型与着色

    总览 在这次编程任务中,我们会进一步模拟现代图形技术.我们在代码中添加了Object Loader(用于加载三维模型), Vertex Shader 与Fragment Shader,并且支持了纹理映 ...

  9. Java Filter型内存马的学习与实践

    完全参考:https://www.cnblogs.com/nice0e3/p/14622879.html 这篇笔记,来源逗神的指点,让我去了解了内存马,这篇笔记记录的是filter类型的内存马 内存马 ...

  10. kviy TextInput 触发事件

    from kivy.uix.widget import Widget from kivy.app import App from kivy.lang import Builder Builder.lo ...