2016-07-25 3 views
0

私はhereからの結果をスクラピーで削り取ろうとしています。問題は、「結果をもっと読み込む」タブがクリックされるまで、すべてのクラスがページに表示されないことです。どのように私はScrapyの次のページにジャンプすることができます

問題はここで見ることができます:

enter image description here

私のコードは次のようになります。

class ClassCentralSpider(CrawlSpider): 
    name = "class_central" 
    allowed_domains = ["www.class-central.com"] 
    start_urls = (
     'https://www.class-central.com/courses/recentlyAdded', 
    ) 
    rules = (
     Rule(
      LinkExtractor(
       # allow=("index\d00\.html",), 
       restrict_xpaths=('//div[@id="show-more-courses"]',) 
      ), 
      callback='parse', 
      follow=True 
     ), 
    ) 

def parse(self, response): 
    x = response.xpath('//span[@class="course-name-text"]/text()').extract() 
    item = ClasscentralItem() 
    for y in x: 
     item['name'] = y 
     print item['name'] 

    pass 
+0

2ページ目のURLはどのように見えますか?そのような場合www.website.com/Recently_Added/2それは本当に簡単な解決策になります。または、実際には、より多くの結果をロードするように表示されるデータを取得しようとしていますか? – SAMO

+0

それは動作しません。私は2ページ目のURLを取得する方法や[次のURLをロードする]を呼び出す方法がわかりません – Yato

+0

これは単なる例に過ぎません。URLが明らかに変化したら、それを悪用する可能性があります。そして、大丈夫ですので、結果を「結果をもっと読み込む」という形で取得しようとしています – SAMO

答えて

1

このウェブサイトのための2ページ目には、AJAX呼び出しを介して生成しているようです。あなたが任意のブラウザの検査ツールのネットワークタブに見ると、あなたが何かわかります

この場合

firebug network tab

をそれが今ではそのURLを思わhttps://www.class-central.com/maestro/courses/recentlyAdded?page=2&_=1469471093134

からJSONファイルを取得しているようです

# so you just need to load it up with 
data = json.loads(response.body) 
# and convert it to scrapy selector - 
sel = Selector(text=data['table']) 
:リターンJSONは、次のページのHTMLコードが含まれています https://www.class-central.com/maestro/courses/recentlyAdded?page=2
:あなただけにそれを離れてトリミングすることができますので、パラメータ _=1469471093134は何もしません10

コード内でこれを複製するには、次のようなコードを試してみてください。

from w3lib.url import add_or_replace_parameter 
def parse(self, response): 
    # check if response is json, if so convert to selector 
    if response.meta.get('is_json',False): 
     # convert the json to scrapy.Selector here for parsing 
     sel = Selector(text=json.loads(response.body)['table']) 
    else: 
     sel = Selector(response) 
    # parse page here for items 
    x = sel.xpath('//span[@class="course-name-text"]/text()').extract() 
    item = ClasscentralItem() 
    for y in x: 
     item['name'] = y 
     print(item['name']) 
    # do next page 
    next_page_el = respones.xpath("//div[@id='show-more-courses']") 
    if next_page_el: # there is next page 
     next_page = response.meta.get('page',1) + 1 
     # make next page url 
     url = add_or_replace_parameter(url, 'page', next_page) 
     yield Request(url, self.parse, meta={'page': next_page, 'is_json': True) 
+0

レスポンス= json.loads(response.load)これを使用してレスポンスをjson to selectorに変換しますか?最後に最後の '}'を返します(url、self.parse、meta = {'page':next_page、 'is_json':True})。 – Yato

+0

私はあなたの解析が何をするかを、私の答えで 'parse()'メソッドを編集しましたが、改ページしました。私はコードをテストしていないが、自分で見つけたときにいくつかのタイプミスを修正できると思う。 – Granitosaurus

+0

別のエラーを修正できる。多分。 :D。ありがとうございました !!! – Yato

関連する問題