2016-08-01 70 views
0

我想抓取链接图像:“http://vnexpress.net/photo/cuoc-song-do-day/nguoi-trung-quoc-ra-be-boi-danh-mat-chuoc-tranh-nong-3445592.html”,但代码只抓取一张图片(在我的计算机中)并抓取所有图片(在我的朋友计算机中)。普莱舍请帮我scrapy只能抓取1张图片

import scrapy 

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

class ImgurSpider(CrawlSpider): 
name = 'imgur' 
allowed_domains = ['vnexpress.net'] 
start_urls = ['http://vnexpress.net/photo/cuoc-song-do-day/nguoi-trung-quoc-ra-be-boi-danh-mat-chuoc-tranh-nong-3445592.html'] 
# rules = [Rule(LinkExtractor(allow=['/*']), 'parse123')] 

def parse(self, response): 
    image = ImgurItem() 
    # image['title'] = response.xpath(\ 
    # "//img[data-notes-url=""]").extract() 
    rel = response.xpath("//div[@id='article_content']//img/@src").extract() 
    image['image_urls'] = [rel[0]] 
    return image 

回答

0
rel = response.xpath("//div[@id='article_content']//img/@src").extract() 
image['image_urls'] = [rel[0]] 

你只需要一个通过指定[0]索引链接。 尝试

image['image_urls'] = rel 

您也可以拆分您的代码以URL解析功能,并下载图像的回调。

+0

哦,是的,谢谢 –

+0

太棒了!你能接受答案吗? – Huxwell