Scrapy只抓取给定页面

时间:2014-05-14 12:34:13

标签: python web-scraping scrapy scrapy-spider

我开始学习scrapy,即时搜索这个问题大约4-5个小时,但可以找到任何东西。有人可以帮帮我吗?我有一个电子商务网站。我只会获取产品页面。其他页面没有,将通过另一页。我给了starturls主页,之后我设置了urls allow()并解析并遵循true但我无法管理它以跟随链接

scrapy crawl loom
2014-05-14 12:33:20+0000 [scrapy] INFO: Scrapy 0.23.0 started (bot: loom)
2014-05-14 12:33:20+0000 [scrapy] INFO: Optional features available: ssl, http11
2014-05-14 12:33:20+0000 [scrapy] INFO: Overridden settings: {'NEWSPIDER_MODULE': 'loom.spiders', 'SPIDER_MODULES': ['loom.spiders'], 'LOG_LEVEL': 'INFO', 'BOT_NAME': 'loom'}
2014-05-14 12:33:20+0000 [scrapy] INFO: Enabled extensions: LogStats, TelnetConsole, CloseSpider, WebService, CoreStats, SpiderState
2014-05-14 12:33:20+0000 [scrapy] INFO: Enabled downloader middlewares: HttpAuthMiddleware, DownloadTimeoutMiddleware, UserAgentMiddleware, RetryMiddleware, DefaultHeadersMiddleware, MetaRefreshMiddleware, HttpCompressionMiddleware, RedirectMiddleware, CookiesMiddleware, ChunkedTransferMiddleware, DownloaderStats
2014-05-14 12:33:20+0000 [scrapy] INFO: Enabled spider middlewares: HttpErrorMiddleware, OffsiteMiddleware, RefererMiddleware, UrlLengthMiddleware, DepthMiddleware
2014-05-14 12:33:20+0000 [scrapy] INFO: Enabled item pipelines:
2014-05-14 12:33:20+0000 [loom] INFO: Spider opened
2014-05-14 12:33:20+0000 [loom] INFO: Crawled 0 pages (at 0 pages/min), scraped 0 items (at 0 items/min)
http://2loom.com
[]
2014-05-14 12:33:20+0000 [loom] INFO: Closing spider (finished)
2014-05-14 12:33:20+0000 [loom] INFO: Dumping Scrapy stats:
        {'downloader/request_bytes': 208,
         'downloader/request_count': 1,
         'downloader/request_method_count/GET': 1,
         'downloader/response_bytes': 6329,
         'downloader/response_count': 1,
         'downloader/response_status_count/200': 1,
         'finish_reason': 'finished',
         'finish_time': datetime.datetime(2014, 5, 14, 12, 33, 20, 824120),
         'log_count/INFO': 7,
         'response_received_count': 1,
         'scheduler/dequeued': 1,
         'scheduler/dequeued/memory': 1,
         'scheduler/enqueued': 1,
         'scheduler/enqueued/memory': 1,
         'start_time': datetime.datetime(2014, 5, 14, 12, 33, 20, 657838)}
2014-05-14 12:33:20+0000 [loom] INFO: Spider closed (finished)

我的蜘蛛:

from scrapy.spider import Spider
from scrapy.selector import Selector
from scrapy.http import Request

from scrapy.utils.response import get_base_url
from scrapy.contrib.spiders import CrawlSpider, Rule
from scrapy.contrib.linkextractors.sgml import SgmlLinkExtractor

class LoomSpider(Spider):
    name = "loom"
    allowed_domains = ["2loom.com"]
    start_urls = [
        "http://2loom.com",
    ]

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

    def parse(self, response):

        print response.url

        sel = Selector(response)
        print sel.xpath('//h1[@itemprop="name"]/text()').extract()

全部谢谢

1 个答案:

答案 0 :(得分:2)

您需要进行一些更改才能使其正常工作:

  • 继承自CrawlSpider而非Spider
  • 提供与callback
  • 不同的parse()

以下是每个链接后面的蜘蛛代码:

class LoomSpider(CrawlSpider):
    name = "loom"
    allowed_domains = ["2loom.com"]
    start_urls = [
        "http://2loom.com",
    ]

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

    def parse_page(self, response):
        print response.url