2

簡単に変更できるシンプルなScrapyCrawlSpiderスクリプトを作成しようとしていますが、リンクエクストラクタのルールを正しく機能させる方法がわかりません。

これが私のコードです:

class LernaSpider(CrawlSpider):
"""Our ad-hoc spider"""

name = "lerna"

def __init__(self, url, allow_follow='.*', deny_follow='', allow_extraction='.*', deny_extraction=''):
    parsed_url = urlparse(url)
    domain = str(parsed_url.netloc)
    self.allowed_domains = [domain]
    self.start_urls = [url]
    self.rules = (
        # Extract links
        # and follow links from them (since no callback means follow=True by default).
        Rule(SgmlLinkExtractor(allow=(allow_follow, ), deny=(deny_follow, ))),

        # Extract links and parse them with the spider's method parse_item
        Rule(SgmlLinkExtractor(allow=(allow_extraction, ), deny=(deny_extraction, )), callback='parse_item'),
    )

    super(LernaSpider, self).__init__()

def parse_item(self, response):

    print 'Crawling... %s' % response.url
    # more stuff here

私はこのコードを持っていますが、許可/拒否ルールを正しく機能させることができず、その理由がわかりません。空の文字列を残すと、すべてが拒否されますか?REだったので、「。*」などを入力した場合にのみ、全面的な拒否が行われると思いました。

どんな助けでもいただければ幸いです。

4

1 に答える 1

3

スパイダーを自分でインスタンス化していますか? 何かのようなもの:

spider = LernaSpider('http://example.com')

そうしないと$scrapy crawl lerna、コマンドラインから実行している場合、コンストラクターの最初のパラメーター (name である必要があります) として url を誤って使用しており、それをスーパーに渡していないためです。多分これを試してください:

class LernaSpider(CrawlSpider):
    """Our ad-hoc spider"""

    name = "lerna"

    def __init__(self, name=None, url=url, allow_follow='.*', deny_follow='', allow_extraction='.*', deny_extraction='', **kw):
        parsed_url = urlparse(url)
        domain = str(parsed_url.netloc)
        self.allowed_domains = [domain]
        self.start_urls = [url]
        self.rules = (
            # Extract links
            # and follow links from them (since no callback means follow=True by default).
            Rule(SgmlLinkExtractor(allow=allow_follow, deny=deny_follow)),

            # Extract links and parse them with the spider's method parse_item
            Rule(SgmlLinkExtractor(allow=allow_extraction, deny=deny_extraction), callback='parse_item'),
        )
        super(LernaSpider, self).__init__(name, **kw)

    def parse_item(self, response):
        print 'Crawling... %s' % response.url
        # more stuff here

正規表現は問題ないように見えます。空の値はすべてを許可し、何も拒否しません。

于 2013-03-22T22:44:36.013 に答える