2017-01-04 10 views
0

私はPythonの新機能(そしてstackoverflowの新機能)で、Scrapyを開始しました。私は別のウェブサイトからいくつかの趣味の製品情報をつかむために探しています。私はチュートリアルを読んだことがあります。私が望むのは、表にリストされている時計の属性ですが、2つ目の表にはすべて同じクラス(「productTitle」)があります。初めてスクラピーを使用してテーブルセットをクロールしようとしました

<table border="0" cellspacing="0" cellpadding="4"> 
    <tbody> 
    <tr> 
     <td class="productTitle creditCardPrice" valign="top"> 
     <strong>Regular Price:</strong> 
     </td> 
     <td valign="top">$9,072</td> 
    </tr> 
    <tr> 
     <td class="productTitle retailPrice" valign="top"> 
     <strong>Retail Price:</strong> 
     </td> 
     <td valign="top">$12,350</td> 
    </tr> 
    <tr> 
     <td class="productTitle itemNumber" valign="top"> 
     <strong>Item Number:</strong> 
     </td> 
     <td valign="top">112555</td> 
    </tr> 
    </tbody> 
</table> 

第二表:

<table border="0" cellpadding="4" cellspacing="0"> 
    <tbody> 
    <tr style="height: 15px;"> 
     <td class="productTitle" style="height: 15px;" valign="top"> .  
     <strong>Manufacturer:</strong> 
     </td> 
     <td style="height: 15px;" valign="top">Rolex</td> 
    </tr> 
    <tr style="height: 30px;"> 
     <td class="productTitle" style="height: 30px;" valign="top"> 
     <strong>Model Name/Number:</strong> 
     </td> 
     <td style="height: 30px;" valign="top">Yacht-Master 116622</td> 
    </tr> 

データのより多くの行があります。 https://www.bobswatches.com/rolex-platinum-yacht-master-116622-pre-owned.html

私の目標は、「クレジットカード価格」、「製造元」、「モデル名/番号」などのラベルが付いた各列を含む.csvファイルにすべてのデータを取得することです。ウェブサイトから自分のお気に入りの時計をクロールし、すべての時計についてこれらのすべての詳細を含むシートを作成します。しかし、スパイダーが異なるページを移動する部分に到達する前に、この1ページを正しくクロールする必要があります。

Scrapyを使用してこれを書き込む方法はありません。私はいくつかの他のstackoverflowの質問やチュートリアルで遊んでいますが、非常に遅い進歩をしています。これは明らかに間違っていますが、私がどこにいるのですか:

def parse(self, response): 
    for row in response.selector.xpath('//table'): 
     yield { 
      'text': row.xpath('./td[1]').extract_first(), 
     } 

    next_page_url = response.xpath('//li[@class="next"]/a/@href').extract_first() 
    if next_page_url is not None: 
     yield scrapy.Request(response.urljoin(next_page_url)) 
+0

http://stackoverflow.com/search?q=Scrapy+csv – McNets

+0

をので、問題は何ですか? – eLRuLL

+0

@eLRuLLそれを明確にするためにさらに追加されました – thaneofcawdor

答えて

0

あなたが正しく理解していれば、構造化されたデータを抽出したいと思いますか?テーブルの行タイトルと行データ?行ごとの各行

  • のためにすべての行が
  • だからそれが正しいxpathセレクタを使用しての問題だタイトルと行のデータを抽出

    1. エキス:

      次の方法でこれを達成することができます。例えば、このようなものは、トリックを行うだろう:

      # find all table rows 
      rows = response.xpath("//tr") 
      for row in rows: 
          title = row.xpath(".//strong/text()").extract_first() 
          text = ''.join(row.xpath(".//td/text()").extract()).strip('. \n') 
          print(title) 
          print(text) 
          print('-'*80) 
      

      リターン:

      Regular Price: 
      $9,072 
      -------------------------------------------------------------------------------- 
      Retail Price: 
      $12,350 
      -------------------------------------------------------------------------------- 
      Item Number: 
      112555 
      -------------------------------------------------------------------------------- 
      Regular Price: 
      $9,072 
      -------------------------------------------------------------------------------- 
      Retail Price: 
      $12,350 
      -------------------------------------------------------------------------------- 
      Item Number: 
      112555 
      -------------------------------------------------------------------------------- 
      Manufacturer: 
      Rolex 
      -------------------------------------------------------------------------------- 
      Model Name/Number: 
      Yacht-Master 116622 
      -------------------------------------------------------------------------------- 
      
    関連する問題