• scrapy递归抓取网页数据


    scrapy spider的parse方法能够返回两种值:BaseItem。或者Request。通过Request能够实现递归抓取。

    假设要抓取的数据在当前页,能够直接解析返回item(代码中带**凝视的行直接改为yield item);

    假设要抓取的数据在当前页指向的页面,则返回Request并指定parse_item作为callback。

    假设要抓取的数据当前页有一部分,指向的页面有一部分(比方博客或论坛,当前页有标题、摘要和url,详情页面有完整内容)这样的情况须要用Request的meta參数把当前页面解析到的数据传到parse_item,后者继续解析item剩下的数据。

    要抓完当前页再抓其他页面(比方下一页),能够返回Request,callback为parse。

    有点奇怪的是:parse不能返回item列表,但作为callback的parse_item却能够。不知道为啥。

    另外。直接extract()得到的文字不包括<a>等子标签的内容。可改为d.xpath('node()').extract()。得到的是包括html的文本。再过滤掉标签就是纯文本了。

    没找到直接得到html的方法。


    from scrapy.spider import Spider
    from scrapy.selector import Selector
    
    from dirbot.items import Article
    
    import json
    import re
    import string
    from scrapy.http import Request
    
    class YouyousuiyueSpider(Spider):
        name = "youyousuiyue2"
        allowed_domains = ["youyousuiyue.sinaapp.com"]
        
        start_urls = [
            'http://youyousuiyue.sinaapp.com',
        ]
            
        def load_item(self, d):
            item = Article()
            title = d.xpath('header/h1/a')
            item['title'] = title.xpath('text()').extract()
            print item['title'][0]
            item['url'] = title.xpath('@href').extract()
            return item
    
        def parse_item(self, response):
            item = response.meta['item']
            
            sel = Selector(response)
            d = sel.xpath('//div[@class="entry-content"]/div')
            item['content'] = d.xpath('text()').extract()
            return item
    
        def parse(self, response):
            """
            The lines below is a spider contract. For more info see:
            http://doc.scrapy.org/en/latest/topics/contracts.html
    
            @url http://youyousuiyue.sinaapp.com
            @scrapes name
            """
            
            print 'parsing ', response.url
            sel = Selector(response)
            articles = sel.xpath('//div[@id="content"]/article')
            for d in articles:
                item = self.load_item(d)
                yield Request(item['url'][0], meta={'item':item}, callback=self.parse_item) # ** or yield item
    
            sel = Selector(response)
            link = sel.xpath('//div[@class="nav-previous"]/a/@href').extract()[0]
            if link[-1] == '4':
                return
            else:
                print 'yielding ', link
                yield Request(link, callback=self.parse)
    

    具体代码见:https://github.com/junglezax/dirbot

    參考:

    http://doc.scrapy.org/en/latest/intro/tutorial.html

    http://www.icultivator.com/p/3166.html


  • 相关阅读:
    源码分析清楚 AbstractQueuedSynchronizer
    Java 并发队列 BlockingQueue
    ReentrantLock以及AQS实现原理
    Java并发之Condition
    Java线程的6种状态及切换
    同步工具类
    Java 并发实践 — ConcurrentHashMap 与 CAS
    Java7/8 中的 HashMap 和 ConcurrentHashMap
    java HashMap、HashTable、ConcurrentHashMap区别
    Mysql的复制原理以及流程
  • 原文地址:https://www.cnblogs.com/yangykaifa/p/6817358.html
Copyright © 2020-2023  润新知