【问题标题】:Scrapy Link Extractor Denying Previously Scraped LinksScrapy Link Extractor 拒绝以前抓取的链接
【发布时间】:2013-02-08 09:24:07
【问题描述】:

我正在使用 Scarpy 中的 CrawlSpider 类构建一个爬虫。我怀疑链接提取器一遍又一遍地循环相同的链接。有没有办法限制链接提取器并拒绝已经被抓取的链接?这可以在拒绝输入中不使用正则表达式的情况下完成吗?

My Rules look like this:

{

rules = (
    #Rule(SgmlLinkExtractor((allow='profile')), follow=True),
    Rule(SgmlLinkExtractor(deny='feedback\.html'),callback='parse_item', follow=True),
    )

}
And my parse_item is:

{

def parse_item(self, response):
    hxs = HtmlXPathSelector(response)
    element = hxs.select('//table[@id="profilehead"]/tr/td/a/@href').extract()
    try:
        open('urls.txt', 'a').write(element[0])
        open('urls.txt', 'a').write('\n')
    except IndexError:
        # Site doesn't have link to another website
        pass

}

【问题讨论】:

  • 如果您发布了完整的蜘蛛代码和您尝试抓取的网站,我们可以查看是否正在解析相同的链接。

标签: python scrapy web-crawler


【解决方案1】:

我认为 scrapy 不会关注已经访问过的链接。但是,如果您想限制某些部分不被遵循,那么您可以尝试这样的事情

restrict_xpaths=('//a[starts-with(@title,"Next ")]')),

http://doc.scrapy.org/en/latest/topics/link-extractors.html

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-01-23
    • 2014-01-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多