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

猜你喜欢

转载自www.cnblogs.com/654321cc/p/8971535.html