3

Scrapy を使用して Web サイトをクロールし、すべてのページを取得していますが、現在のコード ルールでは、" http://www.example.com/some-article/comment-page- 1 "投稿のメイン URL に加えて。これらの不要なアイテムを除外するには、ルールに何を追加できますか? これが私の現在のコードです:

from scrapy.contrib.spiders import CrawlSpider, Rule
from scrapy.contrib.linkextractors.sgml import SgmlLinkExtractor
from scrapy.item import Item

class MySpider(CrawlSpider):
    name = 'crawltest'
    allowed_domains = ['example.com']
    start_urls = ['http://www.example.com']
    rules = [Rule(SgmlLinkExtractor(allow=[r'/\d+']), follow=True), Rule(SgmlLinkExtractor(allow=[r'\d+']), callback='parse_item')]

    def parse_item(self, response):
        #do something
4

1 に答える 1

2

SgmlLinkExtractorと呼ばれるオプションの引数がありますdeny。これは、allow regex が true で deny regex が false の場合にのみルールに一致します

ドキュメントの例:

rules = (
        # Extract links matching 'category.php' (but not matching 'subsection.php')
        # and follow links from them (since no callback means follow=True by default).
        Rule(SgmlLinkExtractor(allow=('category\.php', ), deny=('subsection\.php', ))),

        # Extract links matching 'item.php' and parse them with the spider's method parse_item
        Rule(SgmlLinkExtractor(allow=('item\.php', )), callback='parse_item'),
    )

おそらく、URLに単語が含まれていないことを確認できますcommentか?

于 2013-05-26T16:58:45.657 に答える