2017-04-24 4 views
0

CrawlSpiderを使用して複数のウェブサイトからデータを抽出しようとすると、私は治療に慣れず、固執します。ここでCrawlSpiderでの治療の助けが必要です

は私のコードです:

class ivwSpider(CrawlSpider): 

    name = "ivw-online" 
    allowed_domains = ["ausweisung.ivw-online.de/"] 
    start_urls = ["http://ausweisung.ivw-online.de/index.php?i=1161&a=o44847"] 

    pagelink = LinkExtractor(allow=('index.php?i=1161&a=o\d{5}')) 
    print(pagelink) 
    rules = (Rule(pagelink, callback='parse_item', follow=True),) 

    def parse_item(self, response): 

     sel = Selector(response) 

     item = IVWItem() 
     item["Type"] = sel.xpath('//div[@class ="statistik"]//tr[1]//td/text()')[0].extract() 
     item["Zeitraum"] = sel.xpath('//div[@class ="tabelle"]//tr[1]//div[@style="width:210px; text-align:center;"]/text()')[0].extract() 
     item["Company"] = sel.xpath('//div[@class ="stammdaten"]//tr//td/text()').extract()[-1] 
     item["Video_PIs"] = sel.xpath('//div[@class ="tabelle"]//tr[11]//td[@class ="z5"]/text()').extract() 
     item["Video_Visits"] = sel.xpath('//div[@class ="tabelle"]//tr[11]//td[@class ="z4"]/text()').extract() 
     item["PIs"] = sel.xpath('//div[@class ="statistik"]//tr[3]//td/text()')[1].extract() 
     item["Visits"] = sel.xpath('//div[@class ="statistik"]//tr[1]//td/text()')[1].extract() 

     return item 

コードが実行されると、何も返しません。ルールの定義には問題がありますか?ここのお手伝いが本当に感謝しています!

+0

スパイダーを通常のSpider(CrawlSpiderではなく)としてテストし、start_urlからデータを抽出しました。あなたがすでに推測しているように、問題はCrawlSpiderのルールのようです。あなたがページに従いたいリンクは、私には分かりません。あなたの質問を編集して、hrefが従わなければならない詳細を追加できますか? –

+0

ありがとうございます。私が従うべきリンクは、ivw-ausweisungの異なる競合企業のオンラインデータのすべてのウェブサイトです。 like:http://ausweisung.ivw-online.de/index.php?i=1161&a=o44847,http://ausweisung.ivw-online.de/index.php?i=1161&a=o44851など唯一の違いこれらのURLの間には、i = 1161&a = Oの後の数字が入ります。 –

答えて

0

start_urlはすでに他の競合他社のリストを見つけることができなかった詳細ページですが、私はウェブサイトの階層内でURL http://ausweisung.ivw-online.de/index.php?i=116を開始として上に上がっています。競合他社のリストが長い表があります。

このstart_urlからは、すべての企業のURLを取得することができますので、のようなあなたのコールバックを直接requestsを作成します。

class ivwSpider(scrapy.Spider): 

    name = "ivw-online" 
    allowed_domains = ["ausweisung.ivw-online.de"] 
    start_urls = ["http://ausweisung.ivw-online.de/index.php?i=116"] 

    def parse(self, response): 

     sel_rows = response.xpath('//div[@class="daten"]/div[@class="tabelle"]//tr') 

     for sel_row in sel_rows: 
      url_detail = sel_row.xpath('./td[@class="a_main_txt"][1]/a/@href').extract_first() 
      if url_detail: 
       url = response.urljoin(url_detail) 
       # print url 
       yield scrapy.Request(url, callback=self.parse_item) 

    def parse_item(self, response): 

     sel = Selector(response) 

     item = IVWItem() 
     item["Type"] = sel.xpath('//div[@class ="statistik"]//tr[1]//td/text()')[0].extract() 
     item["Zeitraum"] = sel.xpath('//div[@class ="tabelle"]//tr[1]//div[@style="width:210px; text-align:center;"]/text()')[0].extract() 
     item["Company"] = sel.xpath('//div[@class ="stammdaten"]//tr//td/text()').extract()[-1] 
     item["Video_PIs"] = sel.xpath('//div[@class ="tabelle"]//tr[11]//td[@class ="z5"]/text()').extract() 
     item["Video_Visits"] = sel.xpath('//div[@class ="tabelle"]//tr[11]//td[@class ="z4"]/text()').extract() 
     item["PIs"] = sel.xpath('//div[@class ="statistik"]//tr[3]//td/text()')[1].extract() 
     item["Visits"] = sel.xpath('//div[@class ="statistik"]//tr[1]//td/text()')[1].extract() 

     yield item 

基底クラスは、もは​​やCrawlSpiderしかしSpiderではないことに注意してください。

+0

ありがとうございました!できます –