2016-09-29 33 views
0

我第一次尝试scrapy CrawlSpider子类。我创建了强烈的基础上,文档例如下面的蜘蛛在https://doc.scrapy.org/en/latest/topics/spiders.html#crawlspider-exampleScrapy爬行器设置规则

class Test_Spider(CrawlSpider): 

    name = "test" 

    allowed_domains = ['http://www.dragonflieswellness.com'] 
    start_urls = ['http://www.dragonflieswellness.com/wp-content/uploads/2015/09/'] 

    rules = (
     # Extract links matching 'category.php' (but not matching 'subsection.php') 
     # and follow links from them (since no callback means follow=True by default). 
     # Rule(LinkExtractor(allow=('category\.php',), deny=('subsection\.php',))), 

     # Extract links matching 'item.php' and parse them with the spider's method parse_item 
     Rule(LinkExtractor(allow='.jpg'), callback='parse_item'), 
    ) 

    def parse_item(self, response): 
     self.logger.info('Hi, this is an item page! %s', response.url) 
     print(response.url) 

我试图让蜘蛛循环开始在prescibed目录,然后提取了所有的“.JPG”链接目录,但我看到:

2016-09-29 13:07:35 [scrapy] INFO: Spider opened 
2016-09-29 13:07:35 [scrapy] INFO: Crawled 0 pages (at 0 pages/min), scraped 0 items (at 0 items/min) 
2016-09-29 13:07:35 [scrapy] DEBUG: Telnet console listening on 127.0.0.1:6023 
2016-09-29 13:07:36 [scrapy] DEBUG: Crawled (200) <GET http://www.dragonflieswellness.com/wp-content/uploads/2015/09/> (referer: None) 
2016-09-29 13:07:36 [scrapy] INFO: Closing spider (finished) 

我该如何得到这个工作?

回答

1

首先,使用规则的目的不仅是提取链接,而且最重要的是遵循它们。如果您只想提取链接(并且稍后保存它们),则不必指定蜘蛛规则。另一方面,如果您想要下载图像,请使用pipeline

这就是说,蜘蛛不跟随链接隐藏在LinkExtractor实施的原因:

# common file extensions that are not followed if they occur in links 
IGNORED_EXTENSIONS = [ 
    # images 
    'mng', 'pct', 'bmp', 'gif', 'jpg', 'jpeg', 'png', 'pst', 'psp', 'tif', 
    'tiff', 'ai', 'drw', 'dxf', 'eps', 'ps', 'svg', 

    # audio 
    'mp3', 'wma', 'ogg', 'wav', 'ra', 'aac', 'mid', 'au', 'aiff', 

    # video 
    '3gp', 'asf', 'asx', 'avi', 'mov', 'mp4', 'mpg', 'qt', 'rm', 'swf', 'wmv', 
'm4a', 

    # office suites 
    'xls', 'xlsx', 'ppt', 'pptx', 'pps', 'doc', 'docx', 'odt', 'ods', 'odg', 
'odp', 

    # other 
    'css', 'pdf', 'exe', 'bin', 'rss', 'zip', 'rar', 
] 

编辑:

为了下载在这个例子中使用ImagesPipeline图片:

一下添加到设置:

ITEM_PIPELINES = {'scrapy.pipelines.images.ImagesPipeline': 1} 

IMAGES_STORE = '/home/user/some_directory' # use a correct path 

创建一个新项目:

class MyImageItem(Item): 
    images = Field() 
    image_urls = Field() 

修改您的蜘蛛(添加解析方法):

def parse(self, response): 
     loader = ItemLoader(item=MyImageItem(), response=response) 
     img_paths = response.xpath('//a[substring(@href, string-length(@href)-3)=".jpg"]/@href').extract() 
     loader.add_value('image_urls', [self.start_urls[0] + img_path for img_path in img_paths]) 
     return loader.load_item() 

对于以 “.jpg”,并提取结束所有的HREF的XPath搜索()方法创建一个列表。

装载程序是一种简化创建对象的附加功能,但是您可以不使用它。

请注意,我不是专家,可能会有更好,更优雅的解决方案。然而,这一个工作正常。

+0

谢谢,这确实有帮助,但我仍然试图了解它是如何工作的。我想在这种情况下下载jpg文件,所以我可以要求一个例子,包括管道功能? – user61629

+0

看看我编辑的答案。 – mihal277

+0

感谢您抽出时间。我有兴趣看到人们采取不同的方法。 – user61629