美文网首页
3 Scrapy爬取 (4) items.py

3 Scrapy爬取 (4) items.py

作者: 法号无涯 | 来源:发表于2017-11-16 22:00 被阅读22次

现在要开始学习Item这个东西,或者说学 items.py 这个文件里需要写的代码。

在 items 里去做些处理的话好处是:serialization can be customized and memory leaks are more easy to find if they exist .......

什么意思吗。。。

据我现在所知道的就是我们要在items这个文件里定义我们想要的Field(),并把这个item类load到spider里,做一些事情。load的时候要用一个ItemLoader(item,response)并且把爬到的数据add进这个loader里面。并return这个loader。

l = ItemLoader(item=QuotesSpiderItem(), response=response)

        h1_tag = response.xpath('//h1/a/text()').extract_first()
        tags = response.xpath('//*[@class="tag-item"]/a/text()').extract()

        l.add_value('h1_tag', h1_tag)
        l.add_value('tags', tags)

        return l.load_item()



有其他发现后续补充。

相关文章

网友评论

      本文标题:3 Scrapy爬取 (4) items.py

      本文链接:https://www.haomeiwen.com/subject/pevrvxtx.html