2016-06-22 45 views
1

我正在嘗試使用Selenium和PhantomJS來獲取網站的動態內容。這裏是我的代碼Scrapy中的Selenium + PhantomJS

class judge(Spider): 
    name = "judge" 
    start_urls = ["http://wenshu.court.gov.cn/List/List?sorttype=1&conditions=searchWord+2+AJLX++%E6%A1%88%E4%BB%B6%E7%B1%BB%E5%9E%8B:%E6%B0%91%E4%BA%8B%E6%A1%88%E4%BB%B6"] 

    def init_driver(self): 
     driver = webdriver.Chrome() 
     return driver 

    def parse(self,response): 
     driver = self.init_driver() 
     driver.get(self.start_urls[0]) 
     sel = Selector(text=driver.page_source) 
     self.logger.info(u'---------------Parsing----------------') 
     print sel.xpath("//div[@class='dataItem'][1]/table/tbody/tr[1]/td/div[@class='wstitle']/a/text()").extract() 
     self.logger.info(u'---------------success----------------') 

當我嘗試我的腳本driver = webdriver.Chrome(),sel.xpath(「// DIV [@類=‘的DataItem’]給出所需的內容和一切工作正常。但是,當我改用driver = webdriver.PhantomJS(), sel.xpath(「// div [@ class ='dataItem'] is empty。我嘗試使用WebDriverWait後driver.get()使頁面完全加載,但它不工作

+1

您是否嘗試過在使用幻影來驗證幻像時截圖實際上在動態內容中加載? –

+0

我已經通過'response.page_source'檢查了響應,看起來幻像不會加載動態內容。我不知道發生了什麼問題...... –

+0

@Y_KL你能否整理出來 – Volatil3

回答

0

您可能試試這個:

driver = webdriver.PhantomJS('add your directory of phantomjs here')