第十七章 爬虫scrapy登录与中间件2

文章目录

  • [1. 链接提取](#1. 链接提取)
  • [2. crawlspider全站数据爬取](#2. crawlspider全站数据爬取)
  • [3. Redis简单使用](#3. Redis简单使用)

增加爬取延迟 setting文件中,取消注释DOWNLOAD_DELAY = 3

1. 链接提取

python 复制代码
# 导包
from scrapy.linkextractors import LinkExtracto

def parse(self, resp,**kwargs):
	le = LinkExtractor(restrict_xpaths=('//ul[@class="viewlist_ul"]/li/a',))
	links = le.extract_links(resp)

'''
链接提取方法
def __init__(
        self,
        allow=(),# 允许提取,使用正则
        deny=(), 不允许提取,使用正则
        allow_domains=(), 允许提取的域名
        deny_domains=(),
        restrict_xpaths=(), 使用xpath提取
        tags=("a", "area"),
        attrs=("href",),
        canonicalize=False,
        unique=True,
        process_value=None,
        deny_extensions=None,
        restrict_css=(),
        strip=True,
        restrict_text=None,
    ):

'''

dont_filter 的用法

python 复制代码
    def process_response(self, request, response, spider):
        # Called with the response returned from the downloader.

        # Must either;
        # - return a Response object
        # - return a Request object
        # - or raise IgnoreRequest
        if response.status != 200:  
            request.dont_filter = True #检查代理是否可用,不可用重复请求
            return request
        return response

2. crawlspider全站数据爬取

python 复制代码
# 创建scrapy全站模板
scrapy genspider -t crawl pachpng baidu.com

class ErshouqicheSpider(CrawlSpider):
    name = "ershouqiche"
    allowed_domains = ["che168.com","autohome.com.cn"]
    start_urls = ["https://www.che168.com/china/list/"]

    rules = (Rule(LinkExtractor(restrict_xpaths=('//ul[@class="viewlist_ul"]/li/a',)), callback="parse_item", follow=False),
             Rule(LinkExtractor(restrict_xpaths=('//div[@class="page fn-clear"]/a',)), follow=True))

    def parse_item(self, response):
        print(response.url)
        tittle = response.xpath("//h3[@class='car-brand-name']/text()").extract_first()
        price = response.xpath("//span[@id='overlayPrice']/text()").extract_first()
        #item["domain_id"] = response.xpath('//input[@id="sid"]/@value').get()
        #item["name"] = response.xpath('//div[@id="name"]').get()
        #item["description"] = response.xpath('//div[@id="description"]').get()
        print(tittle,price)

3. Redis简单使用

下载:http://redis.cn/download.html

redis命令

sql 复制代码
#将redis安装到windows服务
redis-server.exe --service-install redis.windows. conf --loglevel verbose
#卸载服务:
redis-server --service-uninstall
#开启服务:
redis-server --service-start
#停止服务:
redis-server --service-stop

配置redis

登录redis

RDM redis可视化工具的安装

https://blog.csdn.net/qq_39715000/article/details/120724800

redis常见数据类型

redis中常见的数据类型有5个.

自增

redis操作

hash

列表

集合

redis教程列表

https://www.runoob.com/redis/redis-sorted-sets.html

python使用resid

pip install redis

相关推荐
深蓝电商API7 天前
将爬虫部署到服务器:Scrapy+Scrapyd 实现定时任务与监控
服务器·爬虫·scrapy
数据牧羊人的成长笔记7 天前
python爬虫scrapy框架使用
爬虫·python·scrapy
MongoVIP9 天前
Scrapy爬虫实战:正则高效解析豆瓣电影
python·scrapy
深蓝电商API10 天前
Scrapy 框架深度解析:架构、组件与工作流程
scrapy·架构
深蓝电商API11 天前
Scrapy 中间件详解:自定义下载器与爬虫的 “拦截器”
爬虫·scrapy·中间件
计算机毕设残哥12 天前
基于Hadoop+Spark的人体体能数据分析与可视化系统开源实现
大数据·hadoop·python·scrapy·数据分析·spark·dash
计算机毕设残哥14 天前
基于Hadoop+Spark的商店购物趋势分析与可视化系统技术实现
大数据·hadoop·python·scrapy·spark·django·dash
万粉变现经纪人20 天前
如何解决 pip install 安装报错 ModuleNotFoundError: No module named ‘tokenizers’ 问题
python·selenium·测试工具·scrapy·beautifulsoup·fastapi·pip
B站_计算机毕业设计之家22 天前
✅ 基于Scrapy与朴素贝叶斯的校园舆情监测与预警系统 Django+B/S架构 可视化大屏 机器学习
python·scrapy·架构·数据分析·django·情感分析·舆情分析
万粉变现经纪人1 个月前
如何解决pip安装报错ModuleNotFoundError: No module named ‘cuml’问题
python·scrapy·beautifulsoup·pandas·ai编程·pip·scipy