2016-03-28 22 views
1

我想改變使用硒phantomjs動態網站scrapyjs刮。但問題是,如果我們在飛濺中編寫點擊事件,它將需要一個收益請求來工作。如果我們提出收益請求,它將呈現第一頁。所以我們在源代碼中看不到點擊事件的變化。即不需要重新呈現網頁。在硒中是可能的。飛濺中是否有相同的功能?如何在沒有任何收益請求的情況下從splash + scrapyjs + scrapy點擊事件後獲取html源代碼?

回答

0

得到了一個使用lua變量的解決方案。我們可以通過splash元參數傳遞變量。 例子:

v = 1 
    yield scrapy.Request(url, meta={'splash': {'endpoint': 'execute','args': {'lua_source': script,'indx':v}},'v':v } , callback=self.parseVariationDetailPage , dont_filter=True) 

我們可以得到我們的 「splash.args.indx」 通過ARGS通過INDX的值。

以下功能顯示元素點擊。

script = """ 
function main(splash) 
    splash:autoload("https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js") 
    z = splash.args.indx 
    assert(splash:go(splash.args.url)) 
    assert(splash:wait(1)) 
    assert(splash:runjs("$('#listChipColor li[z]').click()")) 
    assert(splash:wait(1)) 
    return splash:html() 
end """ 

=====================舊答案在下面=================== ====

如果不使用scrapyjs click事件渲染頁面,我看不到解決方案。

以下是示例代碼及其工作方式。我無法獲得在js中編寫lua變量的解決方案。所以這裏使用一個簡單的邏輯來獲取click元素。

scrapyjs點擊

script = """ 
    function main(splash) 
     splash:autoload("https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js") 
     assert(splash:go(splash.args.url)) 
     assert(splash:runjs("k = window.location.href")) 
     assert(splash:runjs("l = k.length")) 
     assert(splash:wait(1)) 
     assert(splash:runjs("k = k.charAt(l - 1)")) 
     assert(splash:runjs('document.querySelectorAll("ul.colour-swatches-list > li")[k].click();')) 
     assert(splash:wait(1)) 
     return splash:html() 
end """ 

請求

url = url+"vl="+'%s'%v 
yield scrapy.Request(url, self.parseVariationPage,meta={ 
    'splash': { 
     'args': {'lua_source': script},'endpoint': 'execute'}, 
     'url':url,'type':    response.meta['type'],'category':response.meta['category'],'fit':response. meta['fit'],'v':v 
}) 
+0

你能修復代碼的縮進? –

+0

嗨保羅, 我已更正了代碼縮進。 –

相關問題