2017-06-11 17 views
0

の名前を変更する重要:StackOverflowの上で現時点で利用可能なすべての答えはScrapyの以前のバージョンのためのものであるとscrapyする全く新しいscrapy 1.4のPython + Scrapyは、ダウンロードした画像

の最新バージョンでは動作しませんし、 python、私はいくつかのページをこすり、画像をダウンロードしようとしています。画像をダウンロードされているが、彼らはまだ、ファイル名として、元のSHA-1の名前を持っています。 ファイルの名前を変更する方法はわかりませんが、実際にはすべてSHA-1ファイル名があります。

「テスト」と名前を変更しようとしました。scrapy crawl rambopicsをURLのデータとともに実行すると、出力に「テスト」が表示されます。しかし、ファイルは、デスティネーションフォルダ内で名前が変更されません。ここでは、出力のサンプルです:

> 2017-06-11 00:27:06 [scrapy.core.scraper] DEBUG: Scraped from <200 
> http://www.theurl.com/> {'image_urls': 
> ['https://www.theurl.com/-a4Bj-ENjHOY/VyE1mGuJyUI/EAAAAAAAHMk/mw1_H-mEAc0QQEwp9UkTipxNCVR-xdbcgCLcB/s1600/Image%2B%25286%2525.jpg'], 
> 'image_name': ['test'], 'title': ['test'], 'filename': ['test'], 
> 'images': [{'url': 
> 'https://www.theurl.com/-a4Bj-ENjHOY/VyE1mGuJyUI/EAAAAAAAHMk/mw1_H-mEAc0QQEwp9UkTipxNCVR-xdbcgCLcB/s1600/Image%2B%25286%2525.jpg', 
> 'path': 'full/fcbec9bf940b48c248213abe5cd2fa1c690cb879.jpg', 
> 'checksum': '7be30d939a7250cc318e6ef18a6b0981'}]} 

これまでのところ私はすべてのstackoverflowに掲載、さまざまなソリューションを試してみました、scrapyの最新バージョンのためのその質問にだけは明確な答えは2017年に存在しない、それがどのように見えます命題はおそらくほとんどすべて古くなっています。私はPython 3.6でScrapy 1.4を使用しています。

scrapy.cfg

# Automatically created by: scrapy startproject 
# 
# For more information about the [deploy] section see: 
# https://scrapyd.readthedocs.org/en/latest/deploy.html 

[settings] 
default = rambopics.settings 

[deploy] 
#url = http://localhost:6800/ 
project = rambopics 

items.py インポートscrapy

class RambopicsItem(scrapy.Item): 
    # defining items: 
    image_urls = scrapy.Field() 
    images = scrapy.Field() 
    image_name = scrapy.Field() 
    title = scrapy.Field() 
    #pass -- dont realy understand what pass is for 

settings.py

BOT_NAME = 'rambopics' 

SPIDER_MODULES = ['rambopics.spiders'] 
NEWSPIDER_MODULE = 'rambopics.spiders' 


ROBOTSTXT_OBEY = True 


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

IMAGES_STORE = "W:/scraped/" 

pipelines.py

import scrapy 
from scrapy.pipelines.images import ImagesPipeline 

class RambopicsPipeline(ImagesPipeline): 


    def get_media_requests(self, item, info): 

     img_url = item['img_url'] 
     meta = { 
        'filename': item['title'], 
        'title': item['image_name'] 
       } 

     yield Request(url=img_url, meta=meta) 

(クモ)

from rambopics.items import RambopicsItem 
from scrapy.selector import Selector 
import scrapy 


class RambopicsSpider(scrapy.Spider): 
    name = 'rambopics' 
    allowed_domains = ['theurl.com'] 
    start_urls = ['http://www.theurl.com/'] 

    def parse(self, response): 

     for sel in response.xpath('/html'): 
      #img_name = sel.xpath("//h3[contains(@class, 'entry-title')]/a/text()").extract() 
      img_name = 'test' 
      #img_title = sel.xpath("//h3[contains(@class, 'entry-title')]/a/text()").extract() 
      img_title = 'test' 

     for elem in response.xpath("//div[contains(@class, 'entry-content')]"): 
      img_url = elem.xpath("a/@href").extract_first() 


      yield { 
        'image_urls': [img_url], 
        'image_name': [img_name], 
        'title': [img_title], 
        'filename': [img_name] 
       } 

注意をrambopics.py、私が使用する正しいメタ名が最終ダウンロードしたファイルのためにあるのか分かりません名前(ファイル名、イメージ名、タイトルのいずれかがわかりません)。

+0

この質問はすでにここのサイトに答えている:https://stackoverflow.com/a/30002870/1675954、ここで:https://stackoverflow.com/a/6196180/1675954彼らは両方ともかなり包括的答え。基本設定が正しく設定されていることを確認してください。彼らはクロールする前に設定する必要があります。ファイル名の競合を回避しながら、[アイテム欄からコンテンツとScrapy 0.24でダウンロードした画像の名前を変更するhttps://doc.scrapy.org/en/latest/topics/api.html#scrapy.settings.BaseSettings.set –

+0

が重複する可能性を参照してください? ](https://stackoverflow.com/questions/29946989/renaming-downloaded-images-in-scrapy-0-24-with-content-from-an-item-field-while) –

+0

もっと詳しく説明してください他のソリューションとは連携しません。特定のエラーはありますか? –

答えて

0

使用FILE_PATH画像の変更名の方法。

class SaveImagesPipeline(FilesPipeline): 
    def get_media_requests(self, item, info): 
     i = 1 
     for image_url in item['image_urls']: 
      filename = '{}_{}.jpg'.format(item['name_image'], i) 
      yield scrapy.Request(image_url, meta={'filename': filename}) 
      i += 1 
    return 

def file_path(self, request, response=None, info=None): 
    return request.meta['filename'] 
関連する問題