2016-05-20 18 views
1

私は、希望のキーワードの入力を受け取り、検索結果のURLを生成するスクラピースパイダーを持っています。次に、そのURLをクロールして、 'item'内のそれぞれの結果についての望ましい値をスクラップします。私は結果の車のリストに各車に付随する各フルサイズの車の画像リンクのためのURLを私の産出品目内に追加しようとしています。Scrapy Spider Xpath Image URL

私は「ホンダ」としてキーワードを入力するときにクロールされている特定のURLは以下の通りです: Honda search results example

私はXPathを書き込むための正しい方法を考え出す問題を抱えて、その後何のリストが含まれています画像URLの私はスパイダーの「アイテム」に私のコードの最後の部分で私は取得取得します。 今すぐアイテムが.csvファイルに保存され、以下のlkq.pyスパイダーがコマンド "scrapy crawl lkq -o items.csv -t csv"で実行されている場合、図のitems.csvファイルの列はちょうどすべてですイメージURLの代わりに0を使用します。

# -*- coding: utf-8 -*- 
from __future__ import unicode_literals 
import scrapy 
from scrapy.shell import inspect_response 
from scrapy.utils.response import open_in_browser 

keyword = raw_input('Keyword: ') 
url =  'http://www.lkqpickyourpart.com/DesktopModules/pyp_vehicleInventory/getVehicleInventory.aspx?store=224&page=0&filter=%s&sp=&cl=&carbuyYardCode=1224&pageSize=1000&language=en-US' % (keyword,) 
class Cars(scrapy.Item): 
Make = scrapy.Field() 
Model = scrapy.Field() 
Year = scrapy.Field() 
Entered_Yard = scrapy.Field() 
Section = scrapy.Field() 
Color = scrapy.Field() 
Picture = scrapy.Field() 


class LkqSpider(scrapy.Spider): 
name = "lkq" 
allowed_domains = ["lkqpickyourpart.com"] 
start_urls = (
    url, 
) 

def parse(self, response): 
    picture = response.xpath(
     '//href=/text()').extract() 
    section_color = response.xpath(
     '//div[@class="pypvi_notes"]/p/text()').extract() 
    info = response.xpath('//td["pypvi_make"]/text()').extract() 
    for element in range(0, len(info), 4): 
     item = Cars() 
     item["Make"] = info[element] 
     item["Model"] = info[element + 1] 
     item["Year"] = info[element + 2] 
     item["Entered_Yard"] = info[element + 3] 
     item["Section"] = section_color.pop(
      0).replace("Section:", "").strip() 
     item["Color"] = section_color.pop(0).replace("Color:", "").strip() 
     item["Picture"] = picture.pop(0).strip() 
     yield item 

答えて

3

私は本当にhereは非常に良いものです、あなたは'//href=/text()'のようなXPathを使用していた理由は、私が最初にいくつかのXPathのチュートリアルを読んで推薦理解していません。

あなたはすべての画像のURLを取得したい場合は、私は、これはあなたが今

pictures = response.xpath('//img/@src').extract() 

picture.pop(0).strip()はだけなので、あなたのURLの最後を取得し、stripそれ、.extract()はリストを返すことを覚えているだろう欲しいものだと思いますpicturesにはすべての画像リンクが含まれています。必要なものを選択してください。