1、JS問(wèn)題。使用pyspider抓取一些網(wǎng)頁(yè)時(shí),如果網(wǎng)頁(yè)使用JS做渲染,將會(huì)遇到抓取不成功的情況,具體的表現(xiàn)就是在pyspider開(kāi)發(fā)界面左側(cè),web頁(yè)面某些元素顯示不出來(lái)。解決辦法是,使用phantomjs進(jìn)行JS加載和抓取。
注意,下載安裝phantomjs之后,記得將~\phantomjs\bin加入到PATH中,然后在啟動(dòng)pyspider時(shí)使用pyspider all將phantomjs啟動(dòng)。如果是在windows環(huán)境下,系統(tǒng)會(huì)提示FutureWarning : timeout is not supported on your platform.warnings.warn("timeout is not supported on your platform.", FurureWarning),忽略即可。之后即可在crawl()中使用“fetch_type = 'js'”參數(shù),self.crawl(url, callback = self.last_page, fetch_type = 'js')
2、證書(shū)錯(cuò)誤問(wèn)題。在抓取過(guò)程中出現(xiàn)“HTTP 599: SSL certificate problem: self signed certificate in certificate chain”錯(cuò)誤。這是因?yàn)閔ttps協(xié)議需要對(duì)證書(shū)進(jìn)行驗(yàn)證導(dǎo)致,解決方法是,在crawl()方法中使用validate_cert參數(shù),self.crawl(url, callback = self.last_page, validate_cert = False)
3、變量傳遞問(wèn)題。pyspider中如果需要在A函數(shù)中使用B函數(shù)中的變量,會(huì)比較困難。嘗試使用全局變量,在A中修改全局變量,并在B中進(jìn)行引用,未能達(dá)到目的。后查閱文檔,發(fā)現(xiàn)在crawl()方法中有專門(mén)的save參數(shù)解決變量傳遞的問(wèn)題。
在first_page函數(shù)中,self.crawl(url, callback = self.last_page, save = {'current_url': url,? 'path':path_dir}),在last_page函數(shù)中使用response.save['current_url']和response.save['path']即可獲得url和path_dir的值.