문제

Im starting to learn scrapy , im googling that problem about 4-5 hours but coulndt find anything. Anyone can help me ? I have an ecommerce site . I will only fetch products page. The other pages none and will pass another page. I gave starturls homepage, after that i set urls allow() and parse and follow true but i couldnt manage it to follow links

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)

My spider :

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()

Thanks for all

도움이 되었습니까?

해결책

You need to make a couple changes to make it work:

  • inherit from CrawlSpider instead of Spider
  • provide a callback different from parse()

Here's the code of the spider that follows every link:

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
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top