【问题标题】:How to crawl an entire website with Scrapy?如何使用 Scrapy 抓取整个网站?
【发布时间】:2013-03-08 04:10:41
【问题描述】:

我无法爬取整个网站,Scrapy 只是在表面上爬,我想爬得更深。过去 5-6 小时一直在谷歌搜索,但没有任何帮助。我的代码如下:

from scrapy.contrib.spiders import CrawlSpider, Rule
from scrapy.contrib.linkextractors.sgml import SgmlLinkExtractor
from scrapy.selector import HtmlXPathSelector
from scrapy.item import Item
from scrapy.spider import BaseSpider
from scrapy import log

class ExampleSpider(CrawlSpider):
    name = "example.com"
    allowed_domains = ["example.com"]
    start_urls = ["http://www.example.com/"]
    rules = [Rule(SgmlLinkExtractor(allow=()), 
                  follow=True),
             Rule(SgmlLinkExtractor(allow=()), callback='parse_item')
    ]
    def parse_item(self,response):
        self.log('A response from %s just arrived!' % response.url)

【问题讨论】:

  • 刚刚尝试了针对 stackoverflow 的代码 - 我的 ip 被禁止了。它绝对有效! :)
  • @Alexander - 听起来鼓励我进行更多调试 :) :) ... 对不起,IP 禁令队友!
  • 您真的要抓取 example.com 吗?你知道那不是一个真正的网站。
  • 您要爬取哪个网站?
  • "example.com" 仅用于代表目的。我正在尝试抓取 landmarkshops.com

标签: web web-scraping scrapy


【解决方案1】:

规则短路,这意味着链接满足的第一条规则将成为应用的规则,您的第二条规则(带有回调)将不会被调用。

把你的规则改成这样:

rules = [Rule(SgmlLinkExtractor(), callback='parse_item', follow=True)]

【讨论】:

  • @All - 得到它的工作......史蒂文是对的,感谢您的帮助!但我无法爬取整个网站,只能爬取大约 80 个页面。有什么需要纠正的吗?这是我的工作版本:(Rule(SgmlLinkExtractor(allow=('pages/')), follow=True, callback='parse_item'),)
  • 嗨!你介意在这方面提供帮助吗?stackoverflow.com/questions/31630771/…
  • @Steven Almeroth 嗨,Steven 你能帮忙吗stackoverflow.com/questions/37717122/… 我尝试改变规则,但它对我不起作用。
  • dmoz.org 在 href 中没有任何带有“Items”的链接,因此您的规则没有找到任何链接,这就是您的 items.json 文件为空的原因。
【解决方案2】:

解析start_urls时,可以通过标签href解析更深的url。然后,可以在函数parse() 中产生更深层次的请求。 Here is a simple example。最重要的源代码如下所示:

from scrapy.spiders import Spider
from tutsplus.items import TutsplusItem
from scrapy.http    import Request
import re

class MySpider(Spider):
    name            = "tutsplus"
    allowed_domains = ["code.tutsplus.com"]
    start_urls      = ["http://code.tutsplus.com/"]

    def parse(self, response):
        links = response.xpath('//a/@href').extract()

        # We stored already crawled links in this list
        crawledLinks = []

        # Pattern to check proper link
        # I only want to get tutorial posts
        linkPattern = re.compile("^\/tutorials\?page=\d+")

        for link in links:
        # If it is a proper link and is not checked yet, yield it to the Spider
            if linkPattern.match(link) and not link in crawledLinks:
                link = "http://code.tutsplus.com" + link
                crawledLinks.append(link)
                yield Request(link, self.parse)

        titles = response.xpath('//a[contains(@class, "posts__post-title")]/h1/text()').extract()
        for title in titles:
            item = TutsplusItem()
            item["title"] = title
            yield item

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-05-09
    • 2020-10-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多