前情提要:

  一: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请求的更多相关文章

  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. spring cloud中代理服务器zuul的使用

    spring cloud中代理服务器zuul的使用 主流网关:     zuul     kong 基于nginx的API Gateway     nginx+lua 1.新建项目,选择eureka ...

  2. LeetCode 1047. Remove All Adjacent Duplicates In String

    1047. Remove All Adjacent Duplicates In String(删除字符串中的所有相邻重复项) 链接:https://leetcode-cn.com/problems/r ...

  3. 奇妙的算法【4】-汉诺塔&哈夫曼编码

    1,汉诺塔问题[还是看了源码才记起来的,记忆逐渐清晰] 汉诺塔:汉诺塔(又称河内塔)问题是源于印度一个古老传说的益智玩具.大梵天创造世界的时候做了三根金刚石柱子,在一根柱子上从下往上按照大小顺序摞着6 ...

  4. tfs如何为工作项添加变更集

    今天工作中遇到的,可惜之前没怎么用过TFS. 我这是最后一次签入的时候关联了工作项.目的是要把先前签入的绑定到该任务上. 团队自愿管理器->查找历史记录->双击最后一次绑定工作项的变更集- ...

  5. Python中带参数的装饰器

    装饰器本身是用来是为一个函数是实现新的功能,并且不改变原函数的代码以及调用方式. 遇到这样一种问题: 众多函数调用了你写的装饰器,但客户有需求说,我想实现我可以随之控制装饰器是否生效. 那你就不可能在 ...

  6. g++ 生成C++ .so库文件,并调用示例

    Tags: g++ C++ so library   在Linux系统下用g++命令编译C++程序.也可以生成so,a链接库   示例一 编译时链接so库 Test.h 文件内容   Main.cpp ...

  7. 数组的新API

    话不多数,直接上代码: 第一个输出1,2,3,4,5 在函数体中第一个console依次输出1,2,3,4,5 然后再将里面的内容逐个+1,所以第二个输出值为:2,3,4,5,6 但是这都不会改变原数 ...

  8. ORA-3136 问题处理

    Alert 日志报错: Wed May :: *********************************************************************** Fatal ...

  9. Image Processing and Analysis_8_Edge Detection:Edge Detection Revisited ——2004

    此主要讨论图像处理与分析.虽然计算机视觉部分的有些内容比如特 征提取等也可以归结到图像分析中来,但鉴于它们与计算机视觉的紧密联系,以 及它们的出处,没有把它们纳入到图像处理与分析中来.同样,这里面也有 ...

  10. EtherNet/IP 协议应用层使用CIP协议&CIP协议中使用的TLS和DTLS(Network Infrastructure for EtherNet/IPTM: Introduction and Considerations)