2015-10-16 80 views
1

我一直在嘗試使用Scrapy抓取Imgur的圖像,但我遇到了問題。「Scrapy」圖片抓取沒有結果

蜘蛛似乎工作正常,但它沒有去到網站,並做了工作。

我找不到我搞砸了的地方..

items.py

import scrapy 


class ImgurItem(scrapy.Item): 
    title = scrapy.Field() 
    image_urls = scrapy.Field() 
    images = scrapy.Field() 

setting.py

BOT_NAME = 'imgur' 

SPIDER_MODULES = ['imgur.spiders'] 
NEWSPIDER_MODULE = 'imgur.spiders' 
ITEM_PIPELINES = {'scrapy.pipelines.images.ImagesPipeline': 1} 
IMAGES_STORE = '\Users\123\Desktop\images' 

imgur_spider.py

import scrapy 

from scrapy.spiders import Rule, CrawlSpider 
from scrapy.linkextractors import LinkExtractor 
from imgur.items import ImgurItem 

class ImgurSpider(CrawlSpider): 
    name = 'imgur' 
    allowed_domains = ['imgur.com'] 
    start_url = ['http://imgur.com'] 
    rules = [Rule(LinkExtractor(allow=['/gallery/.*']), 'parse_imgur')] 

    def parse_imgur(self, response): 
     image = ImgurItem() 
     image['title'] = response.xpath("//h2[@id='image-title']/text()").extract() 
     rel = response.xpath("//img/@src").extract() 
     image['image_urls'] = ['http:'+rel[0]] 
     return image 

登錄

2015-10-16 16:36:50 [scrapy] INFO: Scrapy 1.0.3 started (bot: imgur) 
2015-10-16 16:36:50 [scrapy] INFO: Optional features available: ssl, http11 
2015-10-16 16:36:50 [scrapy] INFO: Overridden settings: {'NEWSPIDER_MODULE': 'imgur.spiders', 'SPIDER_MODULES': ['imgur.spiders'], 'BOT_NAME': 'imgur'} 
2015-10-16 16:36:50 [scrapy] INFO: Enabled extensions: CloseSpider, TelnetConsole, LogStats, CoreStats, SpiderState 
2015-10-16 16:36:50 [scrapy] INFO: Enabled downloader middlewares: HttpAuthMiddleware, DownloadTimeoutMiddleware, UserAgentMiddleware, RetryMiddleware, DefaultHeadersMiddleware, MetaRefreshMiddleware, HttpCompressionMiddleware, RedirectMiddleware, CookiesMiddleware, ChunkedTransferMiddleware, DownloaderStats 
2015-10-16 16:36:50 [scrapy] INFO: Enabled spider middlewares: HttpErrorMiddleware, OffsiteMiddleware, RefererMiddleware, UrlLengthMiddleware, DepthMiddleware 
2015-10-16 16:36:50 [scrapy] INFO: Enabled item pipelines: ImagesPipeline 
2015-10-16 16:36:50 [scrapy] INFO: Spider opened 
2015-10-16 16:36:50 [scrapy] INFO: Crawled 0 pages (at 0 pages/min), scraped  0 items (at 0 items/min) 
2015-10-16 16:36:50 [scrapy] DEBUG: Telnet console listening on 127.0.0.1:6023 
2015-10-16 16:36:50 [scrapy] INFO: Closing spider (finished) 
2015-10-16 16:36:50 [scrapy] INFO: Dumping Scrapy stats: 
{'finish_reason': 'finished', 
'finish_time': datetime.datetime(2015, 10, 16, 15, 36, 50, 469000), 
'log_count/DEBUG': 1, 
'log_count/INFO': 7, 
'start_time': datetime.datetime(2015, 10, 16, 15, 36, 50, 462000)} 
2015-10-16 16:36:50 [scrapy] INFO: Spider closed (finished) 

回答

1

您的抓取工具正在去http://imgur.com/gallery/Kgb24,然後查找包含「/ gallery /」的所有鏈接(不需要額外的*。)。它沒有找到任何(因爲頁面上沒有),所以爬蟲完成。

編輯: 清理代碼後,它跳出start_url = ...應該是start_urls = ...。事實上,Scrapy沒有任何東西,因爲它沒有提供任何開始。

+0

哦,那是我的不好!忘了改回它..應該是'http://imgur.com/'作爲它應該移動到gallerys的首頁。問題仍然存在。 – Jomasdf

+0

@ user2514200我改變了這種情況,並在提供的代碼中更正了縮進。在我繼續之前,您是否可以檢查以確保它能夠準確地代表您現在在抓取工具中擁有的內容? – Rejected

+0

是的,除了您提到的額外*除外。 – Jomasdf