2017-09-04 11 views
0

セットアップScrapyは、私が<a href="https://www.gumtree.com/property-to-rent/london" rel="nofollow noreferrer">this site</a>からロンドンの住宅の広告をこするよ、一見ランダム点

でクロールを停止します。

ロンドン、特定の地区(セントラルロンドンなど)、特定のサブ地区(Aldgateなど)の3つのエリアサイズで住宅広告を検索できます。

このサイトでは、領域のサイズに関係なく、領域ごとに30の広告ごとに50ページしか確認できません。私。 Xを選択すると、XがCentral LondonかAldgateかにかかわらず、Xで1500の広告を見ることができます。

この質問を書く瞬間、サイトには37,000件以上の広告があります。

私はできるだけ多くの広告を削りたいので、この制限は、準地区レベルで広告を削る必要があることを意味します。そうするには

、私は次のクモ、正常に動作します

# xpath to area/sub area links 
area_links = ('''//*[@id="fullListings"]/div[1]/div/div/nav/aside/''' 
      '''section[1]/div/ul/li/a/@href''') 

class ApartmentSpider(scrapy.Spider): 
    name = 'apartments2' 
    start_urls = [ 
     "https://www.gumtree.com/property-to-rent/london" 
     ] 

    # obtain links to london areas 
    def parse(self, response):     
      for url in response.xpath(area_links).extract(): 
       yield scrapy.Request(response.urljoin(url), 
         callback=self.parse_sub_area)  

    # obtain links to london sub areas 
    def parse_sub_area(self, response):     
      for url in response.xpath(area_links).extract(): 
       yield scrapy.Request(response.urljoin(url), 
         callback=self.parse_ad_overview)  

    # obtain ads per sub area page 
    def parse_ad_overview(self, response):     
      for ads in response.xpath('//*[@id="srp-results"]/div[1]/div/div[2]', 
            ).css('ul').css('li').css('a', 
              ).xpath('@href').extract(): 
       yield scrapy.Request(response.urljoin(ads), 
         callback=self.parse_ad) 

       next_page = response.css(
      '#srp-results > div.grid-row > div > ul > li.pagination-next > a', 
             ).xpath('@href').extract_first() 
       if next_page is not None: 
        next_page = response.urljoin(next_page) 
        yield scrapy.Request(next_page, callback=self.parse) 

    # obtain info per ad 
    def parse_ad(self, response): 

    # here follows code to extract of data per ad 

を書かれています。

それは最初のページから、

  1. エリア面積あたりのそれぞれのエリアのページから
  2. サブ領域、反復処理サブエリアページあたり
  3. 住宅の広告へのリンクを取得し、ありますサブエリアごとのすべてのページ

最後に、個々の広告からデータをスクレイプします。


問題

コードがランダムに一見こする停止し、私はその理由を知りません。

私はそれが多くのリンクとアイテムを掻き集めるように言われているので、限界に達したと思うが、私が正しいかどうかはわからない。

それが停止すると、それは、

{'downloader/request_bytes': 1295950, 
'downloader/request_count': 972, 
'downloader/request_method_count/GET': 972, 
'downloader/response_bytes': 61697740, 
'downloader/response_count': 972, 
'downloader/response_status_count/200': 972, 
'dupefilter/filtered': 1806, 
'finish_reason': 'finished', 
'finish_time': datetime.datetime(2017, 9, 4, 17, 13, 35, 53156), 
'item_scraped_count': 865, 
'log_count/DEBUG': 1839, 
'log_count/ERROR': 5, 
'log_count/INFO': 11, 
'request_depth_max': 2, 
'response_received_count': 972, 
'scheduler/dequeued': 971, 
'scheduler/dequeued/memory': 971, 
'scheduler/enqueued': 971, 
'scheduler/enqueued/memory': 971, 
'spider_exceptions/TypeError': 5, 
'start_time': datetime.datetime(2017, 9, 4, 17, 9, 56, 132388)} 

を述べている私は1つは私が限度か何かをヒットしたかどうか、このから読み取ることができるかどうかわからないんだけど、誰もが知っていなければ、なら、私に知らせてください私はやったし、コードが停止するのを防ぐ方法。

+0

ステータス200の応答しか得られません。何かが本当に間違っている、またはあなたがブロックされている場合は、サービスを利用できない応答(503)などが出ます。コードが途中で停止すると思いますか? – Andras

+0

こんにちはAndras、私は、あなたが何を意味するのか理解できません。 – LucSpan

+0

コードが途中で掻き取りを停止すると思われるのはなぜですか? – Andras

答えて

1

クロールプロセスの完全なログまたは少なくとも部分的なログはトラブルシューティングに役立ちますが、リスクを抱えてこの回答を投稿するつもりです。私はあなたが最後の関数コールバックからあなたの次のページを実行しているときに私は、私は何が起こっているか知っているかなり確信している、過去に同様の問題に遭遇し、あなたのスクリプトを見て問題

def parse_ad_overview(self, response):     
      for ads in response.xpath('//*[@id="srp-results"]/div[1]/div/div[2]', 
            ).css('ul').css('li').css('a', 
              ).xpath('@href').extract(): 
       yield scrapy.Request(response.urljoin(ads), 
         callback=self.parse_ad) 

       next_page = response.css(
      '#srp-results > div.grid-row > div > ul > li.pagination-next > a', 
             ).xpath('@href').extract_first() 
       if next_page is not None: 
        next_page = response.urljoin(next_page) 
        yield scrapy.Request(next_page, callback=self.parse) 

であると仮定しています私は次のページへのリンクがhttpレスポンスであると仮定しています。したがって、コールバックをparse_ad_overviewに変更してください。

関連する問題