2016-05-04 9 views
0

私はscrapyドキュメントのチュートリアルを以下していると私は、以下のサイトからサンプルデータをこすりするために探しています:scrapyでviewコマンドを実行した後 http://www.docteur.ch/generalistes/generalistes_k_ag.htmlScrapyチュートリアルのXPathコードは、テーブルを削る複数回

私は、次のHTMLコードを取得します私がしようとしているテーブルに対してscrape.Theページに各エントリのテーブルで構成されています

<table class="novip"> 
    <tr class="novip"> 
     <td class="novip-portrait-picture" 
     rowspan="5"> 
     <a class="novip-portrait-picture" 
      href="/medecin/baumberger-hans-rudolf-aarau-5000-medecin.html"> 
      <img class="novip-portrait-picture" 
      src="/customer_controlled/pictures/65903/portrait/65903.png" 
      alt="Pas d'image encore" 
      onError="portrait_m_image_failover(this)" /> 
     </a> 
     </td> 
     <td class="novip-left"> 
     <a class="novip-firmen-name" 
      href="/medecin/baumberger-hans-rudolf-aarau-5000-medecin.html" 
      target="_top"> 
      Baumberger&nbsp;Hans Rudolf 
     </a> 
     </td> 
     <td class="novip-right" 
     width="25%"> 
     <a class="novip" 
      href="/medecin/baumberger-hans-rudolf-aarau-5000-medecin.html" 
      target="_top"> 
      rating info:&nbsp;    <img class="novip-inforating" 
      src="/img/general/stars/stars3 " 
      alt="rating info" 
      width="70" height="14" align="bottom" border="0" /> 
     </a> 
     </td> 
    </tr> 
    <tr class="novip"> 
     <td class="novip-left"> 
     Dr. med. Facharzt FMH f&uuml;r Allgemeine Innere Medizin 
     </td> 
    </tr> 
    <tr class="novip"> 
     <td class="novip-left"> 
     Bahnhofstrasse&nbsp;92, 5000&nbsp;Aarau 
     </td> 
     <td class="novip-right-telefon"> 
     t&eacute;l:&nbsp;062 822 46 28 
     </td> 
    </tr> 
    <tr class="novip"> 
     <td class="novip-left-email"> 
     e-mail:&nbsp; 
     <a class="novip-left-send-message-button-inactive" 
      href="/eintrag/fr_keine_mitteilung_moeglich.html"> 
      Envoyer un message 
     </a> 
      &nbsp; 
     <a class="novip-left-make_appointment-button-inactive" 
      href="/eintrag/fr_kein_termin_moeglich.html"> 
      prendre un rendez-vous 
     </a> 
     </td> 
     <td class="novip-right-fax"> 
     fax:&nbsp;062 822 35 20 
     </td> 
    </tr> 
    </table> 

私のクモのために私は、カスタムのXPathとチュートリアルから基本を使用しています:

def parse(self, response): 
     for sel in response.xpath('//tr[@class="novip"]'): 
      item = DocteurItem() 
      item['name'] = sel.xpath('//a[@class="novip-firmen-name"]/text()[normalize-space()]').extract() 
      yield item 

私はJSONで取得した出力は、テーブル内のすべての名前の名前のフィールドを生成しますが、このようなすべてのテーブルからすべての名前を移入:ように

[{"name": ["Name1, Name2, ..... NameN"] 
[{"name": ["Name1, Name2, ..... NameN"] 

とします。どのようにコード/ xpathを変更して名前フィールドに名前を1つだけ移入し、次のテーブルに移動する必要がありますか?

答えて

2

nameコンテキスト固有先頭にドットを付加することによりに対する発現ください:私もextract_first()代わりのextract()を使用してい

for sel in response.xpath('//tr[@class="novip"]'): 
    item = DocteurItem() 
    item['name'] = sel.xpath('.//a[@class="novip-firmen-name"]/text()[normalize-space()]').extract_first() 
    yield item 

注意を。

関連する問題