2017-03-16 12 views
0

私はウェブサイトを削って、ユーザーの個人的な情報(写真はありません)を非常に大まかに取得しようとしていますが、私が修正した公式文書のチュートリアルスパイダーは同じ出力行を4回繰り返しています行。私のScrapyスパイダーがその複製を複製しているのはなぜですか?

私が使用しているコードのコピーは以下の通りです:

私は、コードに含まれてきた例のプロファイルは、偽/スパムアカウントであること。すでに削除されている可能性がある場合は、そのURLをサイトの他のURLに置き換えることができます。次のように

import scrapy 

class DateSpider(scrapy.Spider): 
name = "date" 
start_urls = [ 
    'http://www.pof.com/viewprofile.aspx?profile_id=141659067', 
] 

def parse(self, response): 
    for container in response.xpath('//div[@class="user-details-wide"]'): 
     yield { 
      'Gender': response.xpath("//span[@id='gender']/text()").extract_first(), 
      'Age': response.xpath("//span[@id='age']/text()").extract_first(), 
      'State': response.xpath("//span[@id='state_id']/text()").extract_first(), 
      'Marital status': response.xpath("//span[@id='maritalstatus']/text()").extract_first(), 
      'Body': response.xpath("//span[@id='body']/text()").extract_first(), 
      'Height': response.xpath("//span[@id='height']/text()").extract_first(), 
      'Ethnicity': response.xpath("//span[@id='ethnicity']/text()").extract_first(), 
      'Does drugs?': response.xpath("//span[@id='drugs']/text()").extract_first(), 
      'Smokes?': response.xpath("//span[@id='smoke']/text()").extract_first(), 
      'Drinks?': response.xpath("//span[@id='drink']/text()").extract_first(), 
      'Has children?': response.xpath("//span[@id='haschildren']/text()").extract_first(), 
      'Wants children?': response.xpath("//span[@id='wantchildren']/text()").extract_first(), 
      'Star sign': response.xpath("//span[@id='zodiac']/text()").extract_first(), 
      'Education': response.xpath("//span[@id='college_id']/text()").extract_first(), 
      'Personality': response.xpath("//span[@id='fishtype']/text()").extract_first(), 
     } 

ランニング:

scrapy crawl date -o date.scv 

Iヘッダーの行がまっすぐ後の結果ではなく、空白や重複Iの行が続いている探していた出力を私は現在、

答えて

1

forループを使用する必要はありません。スパン要素を見つけて、彼からすべてのデータを抽出するだけです。

また、私はより便利な治療アイテムを使用することをお勧めします。 空白から抽出されたデータを消去する1つの方法は、xpath関数normalize-space()を使用することです。

import scrapy 
from items import DateSpiderItem 


class DateSpider(scrapy.Spider): 
    name = "date" 
    start_urls = [ 
     'http://www.pof.com/viewprofile.aspx?profile_id=141659067', 
    ] 

    def parse(self, response): 
     item = DateSpiderItem() 
     item['Gender'] = response.xpath(
      "//span[@id='gender']/text()").extract_first() 
     item['Age'] = response.xpath(
      "//span[@id='age']/text()").extract_first() 
     item['State'] = response.xpath(
      "//span[@id='state_id']/text()").extract_first() 
     item['Marital_status'] = response.xpath(
      "normalize-space(//span[@id='maritalstatus']/text())").extract_first() 
     item['Body'] = response.xpath(
      "//span[@id='body']/text()").extract_first() 
     item['Height'] = response.xpath(
      "//span[@id='height']/text()").extract_first() 
     item['Ethnicity'] = response.xpath(
      "//span[@id='ethnicity']/text()").extract_first() 
     item['Does_drugs'] = response.xpath(
      "normalize-space(//span[@id='drugs']/text())").extract_first() 
     item['Smokes'] = response.xpath(
      "//span[@id='smoke']/text()").extract_first() 
     item['Drinks'] = response.xpath(
      "normalize-space(//span[@id='drink']/text())").extract_first() 
     item['Has_children'] = response.xpath(
      "normalize-space(//span[@id='haschildren']/text())").extract_first() 
     item['Wants_children'] = response.xpath(
      "normalize-space(//span[@id='wantchildren']/text())").extract_first() 
     item['Star_sign'] = response.xpath(
      "//span[@id='zodiac']/text()").extract_first() 
     yield item 

アイテムは、ファイル:

class DateSpiderItem(scrapy.Item): 
    Gender = scrapy.Field() 
    Age = scrapy.Field() 
    State = scrapy.Field() 
    Marital_status = scrapy.Field() 
    Body = scrapy.Field() 
    Height = scrapy.Field() 
    Ethnicity = scrapy.Field() 
    Does_drugs = scrapy.Field() 
    Smokes = scrapy.Field() 
    Drinks = scrapy.Field() 
    Has_children = scrapy.Field() 
    Wants_children = scrapy.Field() 
    Star_sign = scrapy.Field() 
    Education = scrapy.Field() 
    Personality = scrapy.Field() 

出力:

enter image description here

+0

は、いくつかのファイルを移動し、名前を変更しなければならなかったが、私が動作するようにあなたのコードを得ました。そしてそれはとてもきれいに見えます!あなたの助けをありがとう、私は本当にそれを感謝します。 –

+0

問題ありません。私はあなたを助けることができてうれしいです。 – vold

関連する問題