前情提要:

  一:scrapy 爬取妹子网 全站 

      知识点: scrapy回调函数的使用

  二: scrapy的各个组件之间的关系解析

Scrapy 框架

Scrapy是用纯Python实现一个为了爬取网站数据、提取结构性数据而编写的应用框架,用途非常广泛。

框架的力量,用户只需要定制开发几个模块就可以轻松的实现一个爬虫,用来抓取网页内容以及各种图片,非常之方便。

Scrapy 使用了 Twisted'twɪstɪd异步网络框架来处理网络通讯,可以加快我们的下载速度,不用自己去实现异步框架,并且包含了各种中间件接口,可以灵活的完成各种需求。

  三:post 的scrapy的使用 

     

  四:首页详情页的数据连续爬取

    例子:4567tv网站

     4.1:setting设置  ,

      注意:设置

        ->1:

      

              ->2:

            

  1. # -*- coding: utf- -*-
  2.  
  3. # Scrapy settings for postdemo1 project
  4. #
  5. # For simplicity, this file contains only settings considered important or
  6. # commonly used. You can find more settings consulting the documentation:
  7. #
  8. # https://doc.scrapy.org/en/latest/topics/settings.html
  9. # https://doc.scrapy.org/en/latest/topics/downloader-middleware.html
  10. # https://doc.scrapy.org/en/latest/topics/spider-middleware.html
  11.  
  12. BOT_NAME = 'postdemo1'
  13.  
  14. SPIDER_MODULES = ['postdemo1.spiders']
  15. NEWSPIDER_MODULE = 'postdemo1.spiders'
  16.  
  17. # Crawl responsibly by identifying yourself (and your website) on the user-agent
  18. USER_AGENT = 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 Safari/537.36'
  19.  
  20. # Obey robots.txt rules
  21. # 不遵循robot协议
  22. ROBOTSTXT_OBEY = False
  23. # 设置错误等级
  24. # LOG_LEVEL ='ERROR'
  25.  
  26. # Configure maximum concurrent requests performed by Scrapy (default: )
  27. #CONCURRENT_REQUESTS =
  28.  
  29. # Configure a delay for requests for the same website (default: )
  30. # See https://doc.scrapy.org/en/latest/topics/settings.html#download-delay
  31. # See also autothrottle settings and docs
  32. #DOWNLOAD_DELAY =
  33. # The download delay setting will honor only one of:
  34. #CONCURRENT_REQUESTS_PER_DOMAIN =
  35. #CONCURRENT_REQUESTS_PER_IP =
  36.  
  37. # Disable cookies (enabled by default)
  38. #COOKIES_ENABLED = False
  39.  
  40. # Disable Telnet Console (enabled by default)
  41. #TELNETCONSOLE_ENABLED = False
  42.  
  43. # Override the default request headers:
  44. #DEFAULT_REQUEST_HEADERS = {
  45. # 'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
  46. # 'Accept-Language': 'en',
  47. #}
  48.  
  49. # Enable or disable spider middlewares
  50. # See https://doc.scrapy.org/en/latest/topics/spider-middleware.html
  51. #SPIDER_MIDDLEWARES = {
  52. # 'postdemo1.middlewares.Postdemo1SpiderMiddleware': ,
  53. #}
  54.  
  55. # Enable or disable downloader middlewares
  56. # See https://doc.scrapy.org/en/latest/topics/downloader-middleware.html
  57. #DOWNLOADER_MIDDLEWARES = {
  58. # 'postdemo1.middlewares.Postdemo1DownloaderMiddleware': ,
  59. #}
  60.  
  61. # Enable or disable extensions
  62. # See https://doc.scrapy.org/en/latest/topics/extensions.html
  63. #EXTENSIONS = {
  64. # 'scrapy.extensions.telnet.TelnetConsole': None,
  65. #}
  66.  
  67. # Configure item pipelines
  68. # See https://doc.scrapy.org/en/latest/topics/item-pipeline.html
  69. #ITEM_PIPELINES = {
  70. # 'postdemo1.pipelines.Postdemo1Pipeline': ,
  71. #}
  72.  
  73. # Enable and configure the AutoThrottle extension (disabled by default)
  74. # See https://doc.scrapy.org/en/latest/topics/autothrottle.html
  75. #AUTOTHROTTLE_ENABLED = True
  76. # The initial download delay
  77. #AUTOTHROTTLE_START_DELAY =
  78. # The maximum download delay to be set in case of high latencies
  79. #AUTOTHROTTLE_MAX_DELAY =
  80. # The average number of requests Scrapy should be sending in parallel to
  81. # each remote server
  82. #AUTOTHROTTLE_TARGET_CONCURRENCY = 1.0
  83. # Enable showing throttling stats for every response received:
  84. #AUTOTHROTTLE_DEBUG = False
  85.  
  86. # Enable and configure HTTP caching (disabled by default)
  87. # See https://doc.scrapy.org/en/latest/topics/downloader-middleware.html#httpcache-middleware-settings
  88. #HTTPCACHE_ENABLED = True
  89. #HTTPCACHE_EXPIRATION_SECS =
  90. #HTTPCACHE_DIR = 'httpcache'
  91. #HTTPCACHE_IGNORE_HTTP_CODES = []
  92. #HTTPCACHE_STORAGE = 'scrapy.extensions.httpcache.FilesystemCacheStorage'

          

    4.2 爬虫文件

       1:爬取首页

        2:爬取详情页

        

    

      4.3数据持久化

      

  1. # -*- coding: utf- -*-
  2.  
  3. # Define your item pipelines here
  4. #
  5. # Don't forget to add your pipeline to the ITEM_PIPELINES setting
  6. # See: https://doc.scrapy.org/en/latest/topics/item-pipeline.html
  7.  
  8. # class Postdemo1Pipeline(object):
  9. # def process_item(self, item, spider):
  10. # print(item)
  11. # return item
  12.  
  13. import pymysql
  14. from redis import Redis
  15.  
  16. class Postdemo1Pipeline(object):
  17. fp = None
  18. def open_spider(self,spider):
  19. print('开始爬虫!')
  20. self.fp = open('./xiaohua.txt','w',encoding='utf-8')
  21. #作用:实现持久化存储的操作
  22. #该方法的item参数就可以接收爬虫文件提交过来的item对象
  23. #该方法每接收一个item就会被调用一次(调用多次)
  24. def process_item(self, item, spider):
  25. name = item['name']
  26. desc = item['desc']
  27. self.fp.write(name+':'+desc+'\n')
  28. #返回值的作用:就是将item传递给下一个即将被执行的管道类
  29. return item
  30.  
  31. def close_spider(self,spider):
  32. print('结束爬虫!')
  33. self.fp.close()
  34.  
  35. class MysqlPipeline(object):
  36. conn = None
  37. cursor = None
  38. def open_spider(self, spider):
  39. #解决数据库字段无法存储中文处理:alter table tableName convert to charset utf8;
  40. self.conn = pymysql.Connect(host='127.0.0.1',port=,user='root',password='',db='test')
  41. print(self.conn)
  42. def process_item(self, item, spider):
  43. self.cursor = self.conn.cursor()
  44. try:
  45. self.cursor.execute('insert into xiahua values ("%s","%s")'%(item['name'],item['img_url']))
  46. self.conn.commit()
  47. except Exception as e:
  48. print(e)
  49. self.conn.rollback()
  50. return item
  51. def close_spider(self, spider):
  52. self.cursor.close()
  53. self.conn.close()
  54.  
  55. class RedisPipeline(object):
  56. conn = None
  57. def open_spider(self, spider):
  58. self.conn = Redis(host='127.0.0.1',port=)
  59. print(self.conn)
  60. def process_item(self, item, spider):
  61. dic = {
  62. 'name':item['name'],
  63. 'img_url':item['img_url']
  64. }
  65. print(dic)
  66. self.conn.lpush('xiaohua',dic)
  67. return item
  68. def close_spider(self, spider):
  69. pass

  五:获取动态加载数据(更改响应数据)

scapy2 爬取全站,以及使用post请求的更多相关文章

  1. 如何用python爬虫从爬取一章小说到爬取全站小说

    前言 文的文字及图片来源于网络,仅供学习.交流使用,不具有任何商业用途,版权归原作者所有,如有问题请及时联系我们以作处理. PS:如有需要Python学习资料的小伙伴可以加点击下方链接自行获取http ...

  2. Python爬取全站妹子图片,差点硬盘走火了!

    在这严寒的冬日,为了点燃我们的热情,今天小编可是给大家带来了偷偷收藏了很久的好东西.大家要注意点哈,我第一次使用的时候,大意导致差点坏了大事哈! 1.所需库安装 2.网站分析 首先打开妹子图的官网(m ...

  3. selenium登录爬取知乎出现:请求异常请升级客户端后重试的问题(用Python中的selenium接管chrome)

    一.问题使用selenium自动化测试爬取知乎的时候出现了:错误代码10001:请求异常请升级客户端后重新尝试,这个错误的产生是由于知乎可以检测selenium自动化测试的脚本,因此可以阻止selen ...

  4. python3爬取全站美眉图片

    爬取网站:https://www.169tp.com/xingganmeinv 该网站美眉图片有数百页,每页24张,共上万张图片,全部爬取下来 import urllib.request import ...

  5. 爬虫再探实战(四)———爬取动态加载页面——请求json

    还是上次的那个网站,就是它.现在尝试用另一种办法——直接请求json文件,来获取要抓取的信息. 第一步,检查元素,看图如下: 过滤出JS文件,并找出包含要抓取信息的js文件,之后就是构造request ...

  6. python爬取全站壁纸代码

    #测试网址:https://www.ivsky.com/bizhi/ #需要安装的库:requests,bs4 #本人是个强迫症患者,为了美观添加数个print(),其并没有实际意义,若是不爽删去即可 ...

  7. Python爬虫---爬取腾讯动漫全站漫画

    目录 操作环境 网页分析 明确目标 提取漫画地址 提取漫画章节地址 提取漫画图片 编写代码 导入需要的模块 获取漫画地址 提取漫画的内容页 提取章节名 获取漫画源网页代码 下载漫画图片 下载结果 完整 ...

  8. scrapy框架之CrawlSpider全站自动爬取

    全站数据爬取的方式 1.通过递归的方式进行深度和广度爬取全站数据,可参考相关博文(全站图片爬取),手动借助scrapy.Request模块发起请求. 2.对于一定规则网站的全站数据爬取,可以使用Cra ...

  9. 爬虫---scrapy全站爬取

    全站爬取1 基于管道的持久化存储 数据解析(爬虫类) 将解析的数据封装到item类型的对象中(爬虫类) 将item提交给管道, yield item(爬虫类) 在管道类的process_item中接手 ...

随机推荐

  1. python第四天---元组和字典的魔法

    # 元组,元素不可以更改.增加.删除 # 元组的第一级元素不可修改.删除.增加 # 一般写元组是,最后加上逗号 tu = (123, "wdsd", 213, (213, 213) ...

  2. JVM GC 算法原理(转)

    出处: https://mp.weixin.qq.com/s/IfUFuwn8dsvMIhTS3V01FA 对于JVM的垃圾收集(GC),这是一个作为Java开发者必须了解的内容,那么,我们需要去了解 ...

  3. go hello world第一个程序

    main 函数所在的包名必须使用main import "fmt"  导入包fmt  fmt包包含了Println方法的定义 func main() 程序运行入口方法和c语言相似 ...

  4. (一)JNDI基础

    一.简介 在Tomcat 4.1.27之后,在服务器上就直接增加了数据源的配置选项,直接在服务器上配置好数据源连接池即可.在J2EE服务器上保存着一个数据库的多个连接.每一个连接通过DataSourc ...

  5. C#从零单排上王者系列---元组

    从零单排系列说明 博主最初的想法是想写个蜕茧成蝶的系列文章,后来觉得博客的表现形式很难做到连贯和系统.所以从本篇博客开始博主会选择书中比较重要和不好理解的知识点并结合自己的实际工作经验来讲解,不再是照 ...

  6. JS 长按 移动端

    实质上,长按的时间不应该过长,因为这有可能与手机系统的部分长按手势产生冲突,但也不宜过短,因为长按时间过短与点击没有任何区别, 理论上,判断长按结束,在手机端上仅设置mouseup动作就可以, < ...

  7. Java02_数据类型

    Java平台: Java API JVM 特点:可跨平台 Java运行机制: 编译(javac.exe) 运行(java.exe) JAVA文件 ---------->class文件(可跨平台的 ...

  8. JS基础_构造函数

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  9. 如何解决js地址栏中传递中文乱码的问题

    目标要求: 实现从A页面跳转至B页面,B页面接收A页面通过地址栏传递过来的中文参数,中文不能出现乱码. A页面部分代码(传递参数): var title = "这是中文"; var ...

  10. Java 之 Map 接口

    一.Map 接口概述 java.util.Map 接口专门用来存放键值对这种对象关系的对象. 下面比较一下 Collection 与 Map 的区别: Collection 中的集合,元素是孤立存在的 ...