2016-07-04 9 views
0

私はScrapyを使用してウェブショップをクロールしています。製品が動的にロードされるため、私はSeleniumを使用してページをクロールしています。私は主な機能のために呼び出されるすべてのカテゴリをこすり始めています。すべてのページをクリックしないでください

各カテゴリをクロールするときに問題が発生します。スパイダーは、最初のページからすべてのデータをスクラップし、ボタンが残っていない限り、次のページに移動するように指示されます。コードは、私はちょうどstart_urlとして1つのカテゴリのURLを入れている場合は正常に動作しますが、私のメインコードでそれを実行すると、それはすべてのページをクリックしないでください。次のボタンをすべてクリックする前に、新しいカテゴリにランダムに切り替わります。

なぜこのような場合に私は考えていません。

import scrapy 
from scrapy import signals 
from scrapy.http import TextResponse 
from scrapy.xlib.pydispatch import dispatcher 
from horni.items import HorniItem 

from selenium import webdriver 
from selenium.webdriver.common.by import By 
from selenium.webdriver.support.wait import WebDriverWait 
from selenium.webdriver.support import expected_conditions as EC 
import time 
from selenium.webdriver.common.keys import Keys 

class horniSpider(scrapy.Spider): 
    name = "final" 
    allowed_domains = ["example.com"] 
    start_urls = ['https://www.example.com'] 

    def parse(self, response): 
     for post in response.xpath('//body'): 
      item = HorniItem() 
      for href in response.xpath('//li[@class="sub"]/a/@href'): 
       item['maincategory'] = response.urljoin(href.extract()) 
       yield scrapy.Request(item['maincategory'], callback = self.parse_subcategories) 

    def parse_subcategories(self, response): 
     item = HorniItem() 
     for href in response.xpath('//li[@class="sub"]/a/@href'): 
      item['subcategory'] = response.urljoin(href.extract()) 
      yield scrapy.Request(item['subcategory'], callback = self.parse_articles) 


    def __init__(self): 
      self.driver = webdriver.Chrome() 
      dispatcher.connect(self.spider_closed, signals.spider_closed) 

    def spider_closed(self, spider): 
      self.driver.close() 

    def parse_articles(self, response): 
      self.driver.get(response.url) 
      response = TextResponse(url=self.driver.current_url, body=self.driver.page_source, encoding='utf-8') 
      item = HorniItem() 
      for sel in response.xpath('//body'): 
       item['title'] = sel.xpath('//div[@id="article-list-headline"]/div/h1/text()').extract() 
       yield item 
      for post in response.xpath('//body'): 
      id = post.xpath('//a[@class="title-link"]/@href').extract() 
      prices = post.xpath('///span[@class="price ng-binding"]/text()').extract() 
       articles = post.xpath('//a[@class="title-link"]/span[normalize-space()]/text()').extract() 
       id = [i.split('/')[-2] for i in id] 
      prices = [x for x in prices if x != u'\xa0'] 
       articles = [w.replace(u'\n', '') for w in articles] 
       result = zip(id, prices, articles) 
       for id, price, article in result: 
         item = HorniItem() 
         item['id'] = id 
       item['price'] = price 
         item['name'] = article 
         yield item 
      while True: 
       next = self.driver.find_element_by_xpath('//div[@class="paging-wrapper"]/a[@class="paging-btn right"]') 
       try: 
         next.click() 
        response = TextResponse(url=self.driver.current_url, body=self.driver.page_source, encoding='utf-8') 
       item = HorniItem() 
        for post in response.xpath('//body'): 
        id = post.xpath('//a[@class="title-link"]/@href').extract() 
        prices = post.xpath('///span[@class="price ng-binding"]/text()').extract() 
         articles = post.xpath('//a[@class="title-link"]/span[normalize-space()]/text()').extract() 
         id = [i.split('/')[-2] for i in id] 
        prices = [x for x in prices if x != u'\xa0'] 
         articles = [w.replace(u'\n', '') for w in articles] 
         result = zip(id, prices, articles) 
         for id, price, article in result: 
          item = HorniItem() 
           item['id'] = id 
         item['price'] = price 
           item['name'] = article 
           yield item 
       except: 
         break 

更新

だから、問題はDOWNLOAD_DELAY -settingであると思われます。ウェブサイトの次のボタンは実際には新しいURLを生成せず、単にJavaスクリプトを実行するため、サイトのURLは変更されません。

答えて

0

私は答えを見つけました:

問題は、ページの内容を動的にNEXT - ボタンをクリックすると、生成されたため、実際のURLを変更していないということでした。 DOWNLOAD_DELAY-プロジェクトの設定と関連して、これは可能なすべてのNEXTボタンをクリックできるかどうかにかかわらず、スパイダーが一定の時間、ページにとどまっていたことを意味しました。

DOWNLOAD_DELAYを高く設定すると、スパイダーは各URLに十分長く留まり、すべてのページをクロールすることができました。

問題は、これがクリックする一切NEXT - ボタンがない場合でも、上のすべての URLを設定時間を待つためにクモを強制することがあります。しかし、よく...

関連する問題