scrapy的调试方法

Parse命令,Scrapy shell,logging

一 Parse命令

  检查spider输出的最基本方法是使用Parse命令。这能让你在函数层上检查spider哥哥部分的效果,其十分灵活并且已用。不过不能在代码中测试。

  https://docs.scrapy.org/en/latest/topics/commands.html#std:command-parse

二 Scrapy shell

  基本使用是配合view 查看scapy拿到的数据。

  高端的用法是。通过scrapy.shell.inspect_response 方法来查看spider的某个位置中被处理的response,以确认期望的response是否到达特定位置。

  效果就相当于,每一个知道到parse的respons,都会支持shell命令,以供查看。

  还是很有用的。

import scrapy

from scrapy.shell import inspect_response
START_URL = 'http://www.521609.com/daxuexiaohua/list31{}.html'
class XiaohuaSpider(scrapy.Spider):
    name = 'xiaohua'

    def start_requests(self):
        yield scrapy.Request(url=START_URL.format(1))
    def parse(self, response):
        inspect_response(response,self)
        items = response.css('div.list_center > ul > li')
        for item in items:
            title = item.css('a.title::text').extract_first()
            print(title)
        next_ = response.css('div.listpage > ol > li:nth-child(14) > a::text')
        if next_.extract_first() == '下一页':
            next_url = response.css('div.listpage > ol > li:nth-child(14) > a::attr(href)').extract_first()
            # print(next_url)
            abs_url = response.urljoin(next_url)
            yield scrapy.Request(url=abs_url)

三 logging

转载于:https://www.cnblogs.com/654321cc/p/8971535.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值