首页 > Scrapy的Request()方法,不用回调函数,直接解析,语法怎么写呢?

Scrapy的Request()方法,不用回调函数,直接解析,语法怎么写呢?

Scrapy的Request()方法,如果用回调函数,可以这样写:

    def parse_page1(self, response):
        return scrapy.Request("http://www.example.com/article-1.html",callback=self.parse_page2)
    
    def parse_page2(self, response):
        title = response.xpath('a/text()').extract()
        link = response.xpath('a/@href').extract()
        desc = response.xpath('text()').extract()
        print title, link, desc

如果不用回调函数,下面这样写对吗?

    article = Request(url="http://www.example.com/article-1.html")

    # 下面这样写对吗?
    title = article.xpath('a/text()').extract()   
    link = article.xpath('a/@href').extract()
    desc = article.xpath('text()').extract()

不要自己从来没试过就来提问啊

article作为Request类的实例, 能有xpath方法吗...

https://scrapy-chs.readthedocs.io/zh_CN/latest/topics/request-response.html

callback If a Request doesn’t specify a callback, the spider’sparse()method will be used.

不给callback调用默认的callback :scrapy.spider.Spider.parsehttps://scrapy-chs.readthedocs.io/zh_CN/latest/topics/spiders.html#scrapy.spider.Spider.parse

【热门文章】
【热门文章】