2017-08-24 90 views
0

我正在搜索這個鏈接,但我沒有成功,我沒有犯任何錯誤,我的價值觀變得空白。如何用Splash刮刮JS頁面?

我正在使用python scrapy和splash。怎麼了 ?有人幫助我嗎?

這是我的蜘蛛代碼:

# -*- coding: utf-8 -*- 
    import scrapy 
    from scrapy_splash import SplashRequest 
    from boom.items import BoomItem 
    from scrapy.selector import HtmlXPathSelector 


    class OrumcekSpider(scrapy.Spider): 
     name = 'orumcek' 
     start_urls = ['example.com'] 

def start_requests(self): 
    for url in self.start_urls: 
     yield SplashRequest(url=url, callback=self.parse, endpoint='render.html') 

def parse(self, response): 
     item = BoomItem() 
     item["BrandName"] = response.xpath("//*[@id='data-item']/div/a/span/text()").extract() 
     item["BrandSector"] = response.xpath("//*[@id='data-item']/div[3]/span/text()").extract() 

     return item 

回答

0

,但它不是id,在圖像我告訴你如何複製選擇的XPath

enter image description here

此頁面的渲染需要時間,你應該等待,直到你找到你想要的元素。

while not splash:select('.your-element') do 
    splash:wait(0.1) 
    end 
+0

謝謝你的回答,我會嘗試你說的方法。 –

+0

@EmreAydınsoy當你使用飛濺或硒進行渲染時,你應該延遲因爲渲染需要時間 – parik

+0

我在scrapy或splash文件中沒有看到類似的東西,謝謝。 –

0

我無法找到id等於data-item任何元素在頁面上,無論是在頁面的源代碼也同時檢查它。但是,有些元素具有屬性data-item。因此,有可能不是用飛濺呈現出了問題,你只需要你有data-item修改XPath來像

item["..."] = response.xpath("//*[@data-item]/...") 
+0

謝謝你的回答。 –

+0

它有助於解決您的問題? –

+0

不,但我有一個新的視角^^ –