2017-09-18 20 views
3

私は、Webページから別の名前を解析するために、小さなスクレーパーをPython Scrapyで作成しました。ページは、ページ付けによってさらに4ページを横断しました。ページ全体での総数は46であるが、36の名前を奪っている。2番目のページの内容を無視した治療

スクレーパーは最初のリンク先ページの内容をスキップしますが、私のスクレーパーではparse_start_url引数を使用して処理しました。

しかし、私は今このスクレーパーで直面している問題は、2ページ目のコンテンツをスキップして残りのすべてを解析することです。最初のページ、3ページ目、4ページ目などを意味します。なぜそれが起こっているのか、それに対処する方法は?前もって感謝します。あなたはstart_urlsに指定されているリンクは、実際に2ページ目のリンクであるので

import scrapy 

class DataokSpider(scrapy.Spider): 

    name = "dataoksp" 
    start_urls = ["https://data.ok.gov/browse?page=1&f[0]=bundle_name%3ADataset&f[1]=im_field_categories%3A4191"] 

    def parse(self, response): 
     for link in response.css('.pagination .pager-item a'): 
      new_link = link.css("::attr(href)").extract_first() 
      yield scrapy.Request(url=response.urljoin(new_link), callback=self.target_page) 

    def target_page(self, response): 
     parse_start_url = self.target_page # I used this argument to capture the content of first page 
     for titles in response.css('.title a'): 
      name = titles.css("::text").extract_first() 
      yield {'Name':name} 

答えて

1

解決策は非常に簡単であることが判明しました。私はすでにそれを修正しました。

import scrapy 

class DataokSpider(scrapy.Spider): 

    name = "dataoksp" 
    start_urls = ["https://data.ok.gov/browse?f[0]=bundle_name%3ADataset&f[1]=im_field_categories%3A4191"] 

    def parse(self, response): 
     for f_link in self.start_urls: 
      yield response.follow(url=f_link, callback=self.target_page) #this is line which fixes the issue 

     for link in response.css('.pagination .pager-item a'): 
      new_link = link.css("::attr(href)").extract_first() 
      yield response.follow(url=new_link, callback=self.target_page) 

    def target_page(self, response): 
     for titles in response.css('.title a'): 
      name = titles.css("::text").extract_first() 
      yield {'Name':name} 

これですべての結果が得られました。

0

:ここ

は私がしようとしているスクリプトです。開いている場合は、現在のページには<a>タグはありません。このコードはあなたを助けるべき

https://data.ok.gov/browse?f[0]=bundle_name%3ADataset&f[1]=im_field_categories%3A4191

::それはあなたがstart_urlsにを指している必要があり、したがって、2ページがtarget_pageに到達していない理由だと

import scrapy 
from scrapy.http import Request 


class DataokspiderSpider(scrapy.Spider): 
    name = 'dataoksp' 
    allowed_domains = ['data.ok.gov'] 
    start_urls = ["https://data.ok.gov/browse?f[0]=bundle_name%3ADataset&f[1]=im_field_categories%3A4191",] 

    def parse(self, response): 
     for titles in response.css('.title a'): 
      name = titles.css("::text").extract_first() 
      yield {'Name':name} 

     next_page = response.xpath('//li[@class="pager-next"]/a/@href').extract_first() 
     if next_page: 
      yield Request("https://data.ok.gov{}".format(next_page), callback=self.parse) 

統計item_scraped_countを参照してください):

{ 
    'downloader/request_bytes': 2094, 
    'downloader/request_count': 6, 
    'downloader/request_method_count/GET': 6, 
    'downloader/response_bytes': 45666, 
    'downloader/response_count': 6, 
    'downloader/response_status_count/200': 6, 
    'finish_reason': 'finished', 
    'finish_time': datetime.datetime(2017, 9, 19, 7, 23, 47, 801934), 
    'item_scraped_count': 46, 
    'log_count/DEBUG': 53, 
    'log_count/INFO': 7, 
    'memusage/max': 47509504, 
    'memusage/startup': 47509504, 
    'request_depth_max': 4, 
    'response_received_count': 6, 
    'scheduler/dequeued': 5, 
    'scheduler/dequeued/memory': 5, 
    'scheduler/enqueued': 5, 
    'scheduler/enqueued/memory': 5, 
    'start_time': datetime.datetime(2017, 9, 19, 7, 23, 46, 59360) 
} 
+0

ありがとうAndrésPérez-Albela H.、あなたの答えです。この解決策は間違いなく機能し、ここに投稿する前に私もそれを試しました。しかし、最初のページ( 'parse_start_url')からデータを解析するための組み込みのスタイルがあります。私は実際にスクリプトをそのガイドラインに基づいて作成することを期待していました。再度、感謝します。 – SIM

+0

@Topto私は私の答えが助けてくれてうれしいです。それがあなたの質問に答えるなら、あなたが私のサポートを大切にする場合には、それを選択された回答とupvoteとして設定してください。 –

+0

@Topto 'parse_start_url'は** CrawlSpider **のためであり、** Spider **のためのものではありません。つまり、それを上書きする必要がある場合は、まずCrawlSpiderから継承する必要があります。 CrawlSpiderは、ルールを必要とせず、私の回答に追加したコード(Spiderだけ)で同じ動作を再現できるので、ユースケースには必要ありません。 –