【问题标题】:Deny certain URLs in Scrapy拒绝 Scrapy 中的某些 URL
【发布时间】:2015-03-17 17:02:06
【问题描述】:

我的 Scrapy 项目中有以下代码:

rules = [
    Rule(LinkExtractor(allow="/uniprot/[OPQ][0-9][A-Z0-9]{3}[0-9]|[A-NR-Z][0-9]([A-Z][A-Z0-9]{2}[0-9]){1,2}"),
        callback="parsethings", follow=False),
    Rule(LinkExtractor(deny_domains=["help", "category", "citations", "taxonomy","diseases", "locations", "docs", "uniref", "proteomes"])),
    Rule(LinkExtractor(deny_domains=[".fasta","?version","?query","?"])),
]

我正在尝试抓取 uniprot (www.uniprot.org) 以获取基因/蛋白质名称和长度。

第一条和最后一条规则可以阻止具有“.fasta”或版本修订号的基因页面的 10,000 个副本,但是,我似乎无法阻止“/help”下的 URL,“ /类别”等。

基本上,我只想抓取 uniprot.org/uniprot 下的 URL。如果我将 allowed_domains 设置为“http://www.uniprot.org/uniprot/”,那么蜘蛛实际上会阻止“www.uniprot.org/uniprot/Q6GZX3”然后死掉。

如何让 scrapy 只抓取 /uniprot 子域中的 URL?

【问题讨论】:

    标签: python web-scraping scrapy screen-scraping


    【解决方案1】:

    将第二条和第三条规则合并为一条:

    rules = [
        Rule(LinkExtractor(allow="/uniprot/[OPQ][0-9][A-Z0-9]{3}[0-9]|[A-NR-Z][0-9]([A-Z][A-Z0-9]{2}[0-9]){1,2}"),
            callback="parsethings", follow=False),
        Rule(LinkExtractor(deny_domains=["help", "category", "citations", "taxonomy","diseases", "locations", "docs", "uniref", "proteomes", ".fasta", "?version", "?query", "?"])),
    ]
    

    【讨论】:

    • Scrapy 仍然使用这条规则抓取这些 URL,尽管它比我上次运行蜘蛛时抓取它们的速度要快得多。
    • 你是说deny_domains中的列表都是uniprot域的子域吗?比如help.uniprot.comcategory.uniprot.comcitations.uniprot.com都是子域?也许我误解了deny_domains 类,但这不需要单个域或域列表,而不是路径吗?
    【解决方案2】:

    未经测试,但您不应该为此使用单一规则:

    rules = [
         Rule(LinkExtractor(allow="/uniprot/[OPQ][0-9][A-Z0-9]{3}[0-9]|[A-NR-Z][0-9]([A-Z][A-Z0-9]{2}[0-9]){1,2}", deny_domains=["help", "category", "citations", "taxonomy","diseases", "locations", "docs", "uniref", "proteomes", ".fasta", "?version", "?query", "?"]),
        callback="parsethings")
    ]
    

    编辑:删除重复的括号 + follow=false

    【讨论】:

    • 除了它不会抓取任何东西,因为它不会跟随任何链接。
    • @crashfocus 嗯,我已经测试过了 - 没有抓取任何东西。您从哪个 URL 开始?
    • @alecxe uniprot.org/uniprot 不幸的是,它在退出之前只抓取了一些基因(但它确实抓取了它们!)
    • @alecxe 更新后的代码确实阻止了不需要的 URL,但它在退出之前只抓取了大约 25 个基因。
    • 这不是因为这些是从 uniprot.org/uniprot/ 页面链接的唯一基因吗?
    【解决方案3】:

    使用deny 代替deny_domains

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-11-12
      相关资源
      最近更新 更多