2016-06-25 5 views
0

方法でparse()スパイダーは4つのURLをクロールし、一部のデータを掻き集めるメソッドparse_dir_contents()に送信しますが、4番目のURLのみが掻き取られています。なぜ他の3つのURLを掻いていないのですか?私はparse_dir_contents機能でforループの必要がないと思ってページを調べてこの蜘蛛の蜘蛛は何ですか?最後のURLだけを掻きます

import scrapy 
from v_one.items import VOneItem 
import json 

class linkedin(scrapy.Spider): 
    name = "linkedin" 
    allowed_domains = ["linkedin.com"] 
    start_urls = [ 
    "https://in.linkedin.com/directory/people-s-1-2-4/", 
    ] 

    def parse(self, response): 

     for href in response.xpath('//*[@id="seo-dir"]/div/div/div/ul/li/a/@href'): 
      url = response.urljoin(href.extract())  
      print "________________"+url 
      yield scrapy.Request(url, callback=self.parse_dir_contents) 



    def parse_dir_contents(self, response): 

     for sel in response.xpath('//*[@id="profile"]'): 
      url = response.url 
      print "____________"+url    
      item = VOneItem() 
      item['name'] = sel.xpath('//*[@id="name"]/text()').extract() 
      item['headline'] = sel.xpath('//*[@id="topcard"]/div/div/div/p/span/text()').extract() 
      item['current'] = sel.xpath('//*[@id="topcard"]/div/div/div/table/tbody/tr/td/ol/li/span/text()').extract() 
      item['education'] = sel.xpath('//*[@id="topcard"]/div/div/div/table/tbody/tr/td/ol/li/a/text()').extract() 
      item['link'] = url 
      yield item 
+0

あなたのコードはすべてのページ/リンクを既に訪問しているので、どのURLが各URLを削っているのかわかりません。また、あなたのxpathsは非常に脆いです、より正確にデータを取得するクラス名がたくさんあります。また、通常はブラウザーによって追加されますので、実際にはそこには存在しないかもしれません –

答えて

0

def parse_dir_contents(self, response): 
     item = VOneItem() 
     item['name'] = response.xpath('//*[@id="name"]/text()').extract() 
     item['headline'] = response.xpath('//*[@id="topcard"]/div/div/div/p/span/text()').extract() 
     item['current'] = response.xpath('//*[@id="topcard"]/div/div/div/table/tbody/tr/td/ol/li/span/text()').extract() 
     item['education'] = response.xpath('//*[@id="topcard"]/div/div/div/table/tbody/tr/td/ol/li/a/text()').extract() 
     item['link'] = response.url 
     return item 

これが問題を解決するかどうかを確認してください。

+0

このアプローチは誤解を招きますか?黙って投票しないでください。理由を指定します。 –

関連する問題