2016-09-24 23 views
0

我無法使用scapry按照「下一頁」的鏈接 - 根據日誌它是指自己而不是「下一頁」的網址。下面是代碼:Scrapy引用回原來的頁面而不是下一頁

import scrapy 

class QuotesSpider(scrapy.Spider): 
name = "quotes2" 
start_urls = [ 
     'http://search.jeffersondeeds.com/pdetail.php?instnum=2016230701&year=2016&db=0&cnum=20', 
] 

def parse(self, response): 
    for quote in response.xpath('//div'): 
     yield{ 
      'record' : quote.select(".//span/text()").extract() 
     } 

    next_page = response.xpath('//*[@id="nextpage"]/a/@href').extract() 

    if next_page is not None: 
     print("GOOOO BUCKS!!") 
     next_page = response.urljoin(next_page) 
     yield scrapy.Request(next_page, callback=self.parse) 
    else: 
     print("Ahhh fooey!") 

中的XPath看起來是正確的:

enter image description here

但被捕獲爲next_page的網址是原來的URL(starts_urls)

+0

有什麼'print'你設定的輸出? – eLRuLL

+0

它打印「GOOOO BUCKS !!」。我添加了一個print(next_page),它打印出原始URL(不是鏈接中的那個) –

+1

請調試你的代碼,next_page不是None,但它是一個空的列表,並且我看到了正在生成的實際下一頁鏈接一個javascript裏面的'//表// //腳本/文本()' – eLRuLL

回答

1

next_page ISN」沒有,但它是一個空的列表。

現在正在裏面'//table//script/text()'

一個javascript產生的nextpage鏈接,您可以用得到它:response.xpath('//table//script/text()').re_first("href=\\'(pdetail.*)\\'>")

+0

胡人 - 夥計。你搖滾!完善。 –

相關問題