scapy2 爬取全站,以及使用post请求
前情提要:
一:scrapy 爬取妹子网 全站
知识点: scrapy回调函数的使用
二: scrapy的各个组件之间的关系解析
Scrapy 框架
Scrapy是用纯Python实现一个为了爬取网站数据、提取结构性数据而编写的应用框架,用途非常广泛。
框架的力量,用户只需要定制开发几个模块就可以轻松的实现一个爬虫,用来抓取网页内容以及各种图片,非常之方便。
Scrapy 使用了 Twisted'twɪstɪd异步网络框架来处理网络通讯,可以加快我们的下载速度,不用自己去实现异步框架,并且包含了各种中间件接口,可以灵活的完成各种需求。
三:post 的scrapy的使用
四:首页详情页的数据连续爬取
例子:4567tv网站
4.1:setting设置 ,
注意:设置
->1:
->2:
- # -*- coding: utf- -*-
- # Scrapy settings for postdemo1 project
- #
- # For simplicity, this file contains only settings considered important or
- # commonly used. You can find more settings consulting the documentation:
- #
- # https://doc.scrapy.org/en/latest/topics/settings.html
- # https://doc.scrapy.org/en/latest/topics/downloader-middleware.html
- # https://doc.scrapy.org/en/latest/topics/spider-middleware.html
- BOT_NAME = 'postdemo1'
- SPIDER_MODULES = ['postdemo1.spiders']
- NEWSPIDER_MODULE = 'postdemo1.spiders'
- # Crawl responsibly by identifying yourself (and your website) on the user-agent
- USER_AGENT = 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 Safari/537.36'
- # Obey robots.txt rules
- # 不遵循robot协议
- ROBOTSTXT_OBEY = False
- # 设置错误等级
- # LOG_LEVEL ='ERROR'
- # Configure maximum concurrent requests performed by Scrapy (default: )
- #CONCURRENT_REQUESTS =
- # Configure a delay for requests for the same website (default: )
- # See https://doc.scrapy.org/en/latest/topics/settings.html#download-delay
- # See also autothrottle settings and docs
- #DOWNLOAD_DELAY =
- # The download delay setting will honor only one of:
- #CONCURRENT_REQUESTS_PER_DOMAIN =
- #CONCURRENT_REQUESTS_PER_IP =
- # Disable cookies (enabled by default)
- #COOKIES_ENABLED = False
- # Disable Telnet Console (enabled by default)
- #TELNETCONSOLE_ENABLED = False
- # Override the default request headers:
- #DEFAULT_REQUEST_HEADERS = {
- # 'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
- # 'Accept-Language': 'en',
- #}
- # Enable or disable spider middlewares
- # See https://doc.scrapy.org/en/latest/topics/spider-middleware.html
- #SPIDER_MIDDLEWARES = {
- # 'postdemo1.middlewares.Postdemo1SpiderMiddleware': ,
- #}
- # Enable or disable downloader middlewares
- # See https://doc.scrapy.org/en/latest/topics/downloader-middleware.html
- #DOWNLOADER_MIDDLEWARES = {
- # 'postdemo1.middlewares.Postdemo1DownloaderMiddleware': ,
- #}
- # Enable or disable extensions
- # See https://doc.scrapy.org/en/latest/topics/extensions.html
- #EXTENSIONS = {
- # 'scrapy.extensions.telnet.TelnetConsole': None,
- #}
- # Configure item pipelines
- # See https://doc.scrapy.org/en/latest/topics/item-pipeline.html
- #ITEM_PIPELINES = {
- # 'postdemo1.pipelines.Postdemo1Pipeline': ,
- #}
- # Enable and configure the AutoThrottle extension (disabled by default)
- # See https://doc.scrapy.org/en/latest/topics/autothrottle.html
- #AUTOTHROTTLE_ENABLED = True
- # The initial download delay
- #AUTOTHROTTLE_START_DELAY =
- # The maximum download delay to be set in case of high latencies
- #AUTOTHROTTLE_MAX_DELAY =
- # The average number of requests Scrapy should be sending in parallel to
- # each remote server
- #AUTOTHROTTLE_TARGET_CONCURRENCY = 1.0
- # Enable showing throttling stats for every response received:
- #AUTOTHROTTLE_DEBUG = False
- # Enable and configure HTTP caching (disabled by default)
- # See https://doc.scrapy.org/en/latest/topics/downloader-middleware.html#httpcache-middleware-settings
- #HTTPCACHE_ENABLED = True
- #HTTPCACHE_EXPIRATION_SECS =
- #HTTPCACHE_DIR = 'httpcache'
- #HTTPCACHE_IGNORE_HTTP_CODES = []
- #HTTPCACHE_STORAGE = 'scrapy.extensions.httpcache.FilesystemCacheStorage'
4.2 爬虫文件
1:爬取首页
2:爬取详情页
4.3数据持久化
- # -*- coding: utf- -*-
- # Define your item pipelines here
- #
- # Don't forget to add your pipeline to the ITEM_PIPELINES setting
- # See: https://doc.scrapy.org/en/latest/topics/item-pipeline.html
- # class Postdemo1Pipeline(object):
- # def process_item(self, item, spider):
- # print(item)
- # return item
- import pymysql
- from redis import Redis
- class Postdemo1Pipeline(object):
- fp = None
- def open_spider(self,spider):
- print('开始爬虫!')
- self.fp = open('./xiaohua.txt','w',encoding='utf-8')
- #作用:实现持久化存储的操作
- #该方法的item参数就可以接收爬虫文件提交过来的item对象
- #该方法每接收一个item就会被调用一次(调用多次)
- def process_item(self, item, spider):
- name = item['name']
- desc = item['desc']
- self.fp.write(name+':'+desc+'\n')
- #返回值的作用:就是将item传递给下一个即将被执行的管道类
- return item
- def close_spider(self,spider):
- print('结束爬虫!')
- self.fp.close()
- class MysqlPipeline(object):
- conn = None
- cursor = None
- def open_spider(self, spider):
- #解决数据库字段无法存储中文处理:alter table tableName convert to charset utf8;
- self.conn = pymysql.Connect(host='127.0.0.1',port=,user='root',password='',db='test')
- print(self.conn)
- def process_item(self, item, spider):
- self.cursor = self.conn.cursor()
- try:
- self.cursor.execute('insert into xiahua values ("%s","%s")'%(item['name'],item['img_url']))
- self.conn.commit()
- except Exception as e:
- print(e)
- self.conn.rollback()
- return item
- def close_spider(self, spider):
- self.cursor.close()
- self.conn.close()
- class RedisPipeline(object):
- conn = None
- def open_spider(self, spider):
- self.conn = Redis(host='127.0.0.1',port=)
- print(self.conn)
- def process_item(self, item, spider):
- dic = {
- 'name':item['name'],
- 'img_url':item['img_url']
- }
- print(dic)
- self.conn.lpush('xiaohua',dic)
- return item
- def close_spider(self, spider):
- pass
五:获取动态加载数据(更改响应数据)
scapy2 爬取全站,以及使用post请求的更多相关文章
- 如何用python爬虫从爬取一章小说到爬取全站小说
前言 文的文字及图片来源于网络,仅供学习.交流使用,不具有任何商业用途,版权归原作者所有,如有问题请及时联系我们以作处理. PS:如有需要Python学习资料的小伙伴可以加点击下方链接自行获取http ...
- Python爬取全站妹子图片,差点硬盘走火了!
在这严寒的冬日,为了点燃我们的热情,今天小编可是给大家带来了偷偷收藏了很久的好东西.大家要注意点哈,我第一次使用的时候,大意导致差点坏了大事哈! 1.所需库安装 2.网站分析 首先打开妹子图的官网(m ...
- selenium登录爬取知乎出现:请求异常请升级客户端后重试的问题(用Python中的selenium接管chrome)
一.问题使用selenium自动化测试爬取知乎的时候出现了:错误代码10001:请求异常请升级客户端后重新尝试,这个错误的产生是由于知乎可以检测selenium自动化测试的脚本,因此可以阻止selen ...
- python3爬取全站美眉图片
爬取网站:https://www.169tp.com/xingganmeinv 该网站美眉图片有数百页,每页24张,共上万张图片,全部爬取下来 import urllib.request import ...
- 爬虫再探实战(四)———爬取动态加载页面——请求json
还是上次的那个网站,就是它.现在尝试用另一种办法——直接请求json文件,来获取要抓取的信息. 第一步,检查元素,看图如下: 过滤出JS文件,并找出包含要抓取信息的js文件,之后就是构造request ...
- python爬取全站壁纸代码
#测试网址:https://www.ivsky.com/bizhi/ #需要安装的库:requests,bs4 #本人是个强迫症患者,为了美观添加数个print(),其并没有实际意义,若是不爽删去即可 ...
- Python爬虫---爬取腾讯动漫全站漫画
目录 操作环境 网页分析 明确目标 提取漫画地址 提取漫画章节地址 提取漫画图片 编写代码 导入需要的模块 获取漫画地址 提取漫画的内容页 提取章节名 获取漫画源网页代码 下载漫画图片 下载结果 完整 ...
- scrapy框架之CrawlSpider全站自动爬取
全站数据爬取的方式 1.通过递归的方式进行深度和广度爬取全站数据,可参考相关博文(全站图片爬取),手动借助scrapy.Request模块发起请求. 2.对于一定规则网站的全站数据爬取,可以使用Cra ...
- 爬虫---scrapy全站爬取
全站爬取1 基于管道的持久化存储 数据解析(爬虫类) 将解析的数据封装到item类型的对象中(爬虫类) 将item提交给管道, yield item(爬虫类) 在管道类的process_item中接手 ...
随机推荐
- python第四天---元组和字典的魔法
# 元组,元素不可以更改.增加.删除 # 元组的第一级元素不可修改.删除.增加 # 一般写元组是,最后加上逗号 tu = (123, "wdsd", 213, (213, 213) ...
- JVM GC 算法原理(转)
出处: https://mp.weixin.qq.com/s/IfUFuwn8dsvMIhTS3V01FA 对于JVM的垃圾收集(GC),这是一个作为Java开发者必须了解的内容,那么,我们需要去了解 ...
- go hello world第一个程序
main 函数所在的包名必须使用main import "fmt" 导入包fmt fmt包包含了Println方法的定义 func main() 程序运行入口方法和c语言相似 ...
- (一)JNDI基础
一.简介 在Tomcat 4.1.27之后,在服务器上就直接增加了数据源的配置选项,直接在服务器上配置好数据源连接池即可.在J2EE服务器上保存着一个数据库的多个连接.每一个连接通过DataSourc ...
- C#从零单排上王者系列---元组
从零单排系列说明 博主最初的想法是想写个蜕茧成蝶的系列文章,后来觉得博客的表现形式很难做到连贯和系统.所以从本篇博客开始博主会选择书中比较重要和不好理解的知识点并结合自己的实际工作经验来讲解,不再是照 ...
- JS 长按 移动端
实质上,长按的时间不应该过长,因为这有可能与手机系统的部分长按手势产生冲突,但也不宜过短,因为长按时间过短与点击没有任何区别, 理论上,判断长按结束,在手机端上仅设置mouseup动作就可以, < ...
- Java02_数据类型
Java平台: Java API JVM 特点:可跨平台 Java运行机制: 编译(javac.exe) 运行(java.exe) JAVA文件 ---------->class文件(可跨平台的 ...
- JS基础_构造函数
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...
- 如何解决js地址栏中传递中文乱码的问题
目标要求: 实现从A页面跳转至B页面,B页面接收A页面通过地址栏传递过来的中文参数,中文不能出现乱码. A页面部分代码(传递参数): var title = "这是中文"; var ...
- Java 之 Map 接口
一.Map 接口概述 java.util.Map 接口专门用来存放键值对这种对象关系的对象. 下面比较一下 Collection 与 Map 的区别: Collection 中的集合,元素是孤立存在的 ...