2016-10-11 110 views
0

我已经尝试了谷歌和堆栈溢出解决方案中的每个搜索结果,但我无法获得解决方案。 我创建一个scrapy提取图片,请在下面找到Scrapy无法获取图片网址,也无法下载图片

我items.py

class MyntraItem(scrapy.Item): 
    product_urls=scrapy.Field() 
    files=scrapy.Field() 
    image_urls=scrapy.Field() 
    images = scrapy.Field() 

我的settings.py

BOT_NAME = 'hello' 

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

FILES_STORE = '/home/swapnil/Desktop/AI/myntra/' 
ITEM_PIPELINES = { 
    #'myntra.pipelines.SomePipeline': 300, 
    'scrapy.pipelines.images.FilesPipeline': 1, 
} 

我first.py代码

class FirstSpider(CrawlSpider): 
    name = "first" 
    allowed_domains = ["myntra.com"] 
    start_urls = [ 
     'http://www.myntra.com/men-sports-tshirts-menu?src=tNav&f=Pattern_article_attr%3Astriped', 
    ] 
    rules = [Rule(LinkExtractor(restrict_xpaths=['//*[@class="product-link"]']),callback='parse_lnk',follow=True)] 
    #rules = [Rule(LinkExtractor(allow=['.*']),callback='parse_lnk',follow=True)] 

    def parse_lnk(self, response): 
     item=MyntraItem() 
     item['product_urls']=response.url 
     item['files']=response.xpath('//*[@class="thumbnails-selected-image"]/@src') 
     item['image_urls']=item['files'] 
     #print '666666666666666666',item['files'] 
     return item 

请帮助:我的意图是下载图像。

回答

0

默认情况下,FilesPipeline预计文件URL的值可从an item's "file_urls" key的值中获得。

(...)如果蜘蛛返回与该网址键("file_urls“或 "image_urls",对文件或图像管道分别),该 管道将把各自项下的结果("files"或字典"images")。

看来你正在使用"product_urls"。要更改管道查找网址,您需要设置FILES_URLS_FIELD = "product_urls"

+0

在我的代码'product_urls'是所有的产品链接而不是图像链接和项目['文件'] = response.xpath('// * [@ class =“thumbnails-selected-image”]/@ src' )这段代码有关于需要下载的图像的信息,并且正如您所建议的,使用image_urls更改名称的方式比它不起作用。请检查我看到的更新代码 –

+0

。您正在使用'FilesPipeline',所以您需要1)在项目定义中使用'file_urls'字段,并在返回项目时填充它,或者2)告诉管道寻找另一个字段,而是查找“设置”。 py'需要定义'FILES_URLS_FIELD =“images_urls”'。另一种方式是在代码中进行较少的更改,即加载“ImagesPipeline”而不是“FilesPipeline”。另请注意'// @ [@ class =“thumbnails-selected-image”]/@ src'似乎不会从产品页面生成图像。 –

0

使用ImagesPipeline改为,并使用正则表达式提取图像。

在我first.py

item['files']= re.findall('front":\{"path":"(.+?)"', response.body) 

在settings.py

IMAGES_STORE = '/home/swapnil/Desktop/AI/myntra/' 

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

这简直妙不可言。

+0

我尝试了你的建议,然后尝试[链接](http://www.myntra.com/tshirts/fila/fila-men-green-eagle-printed-round-neck-t-shirt/1502783/buy?src= search&uq = false&q = C%3A%2FUsers%2Fsharm_000%2FAppData%2FLocal%2FTemp%2Ftmpqshvkx.html&p = 4)在scrapy shell中查看这个链接,它没有给出任何内容conteny可能这是一个问题。 –

+0

我不确定是什么问题,但是,图像网址在呈现到浏览器中之前实际上存储在JavaScript变量中;因此您需要使用** HtmlXPathSelector **中的re方法或直接使用** re **模块来提取图像url。 –