2015-04-01 127 views
2

我想取的聯繫,從這個網站在塞浦路斯的全部住宿onclick事件: http://www.zoover.nl/cyprus調用與beautifulsoup蟒蛇

到目前爲止,我可以檢索其已經顯示了前15。所以現在我必須調用點擊「volgende」鏈接。不過,我不知道該怎麼做,並且在源代碼中,我無法追蹤被調用的函數,例如, ......喜歡張貼在這裏: Issues with invoking "on click event" on the html page using beautiful soup in Python

我只需要發生「點擊」的步驟,因此我可以獲取下15個鏈接等。

有人知道如何提供幫助嗎? 已經感謝!

編輯:

我的代碼看起來像現在這樣:

def getZooverLinks(country): 
    zooverWeb = "http://www.zoover.nl/" 
    url = zooverWeb + country 
    parsedZooverWeb = parseURL(url) 
    driver = webdriver.Firefox() 
    driver.get(url) 

    button = driver.find_element_by_class_name("next") 
    links = [] 
    for page in xrange(1,3): 
     for item in parsedZooverWeb.find_all(attrs={'class': 'blue2'}): 
      for link in item.find_all('a'): 
       newLink = zooverWeb + link.get('href') 
       links.append(newLink) 
     button.click()' 

,我得到以下錯誤:

selenium.common.exceptions.StaleElementReferenceException:消息:元素不再連接到DOM Stacktrace: at fxdriver.cache.getElementAt(resource://fxdriver/modules/web-element-cache.js:8956) at Utils.getElementAt(file:/// var/folders/n4/fhvh qlmx23s8ppxbrxrpws3c0000gn/T/tmpKFL43_/extensions/[email protected]/components/command-processor.js:8546) at fxdriver.preconditions.visible(file:/// var/folders/n4/fhvhqlmx23s8ppxbrxrpws3c0000gn/T/tmpKFL43_/extensions /[email protected]/components/command-processor.js:9585) at DelayedCommand.prototype.checkPreconditions_(file:///var/folders/n4/fhvhqlmx23s8ppxbrxrpws3c0000gn/T/tmpKFL43_/extensions/[email protected]/ components/command-processor.js:12257) at DelayedCommand.prototype.executeInternal_/h(file:///var/folders/n4/fhvhqlmx23s8ppxbrxrpws3c0000gn/T/tmpKFL43_/extensions/[email protected]/components/command-processor .js:12274) at DelayedCommand.prototype.executeInternal_(file:///var/folders/n4/fhvhqlmx23s8ppxbrxrpws3c0000gn/T/tmpKFL43_/extensions/[email protected]/components/command-processor.js:12279) 在DelayedCommand.prototype.execute/<(文件:///var/folders/n4/fhvhqlmx23s8ppxbrxrpws3c0000gn/T/tmpKFL43_/extensions/[email protected]/components/command-processor.js:12221)

我'm confused:/

回答

3

儘管使用Beautifulsoup的evaluateJavaScript方法試圖做到這一點可能很誘人,但最終Beautifulsoup是parser而不是交互式Web瀏覽客戶端。

你應該認真考慮用硒來解決這個問題,如this answer所簡述的那樣。硒有很好的Python bindings

您可以使用硒查找元素並單擊它,然後將頁面傳遞給Beautifulsoup,並使用您現有的代碼來獲取鏈接。

或者,您可以使用onclick處理程序中列出的Javascript。我從源頭上取得這個:EntityQuery('Ns=pPopularityScore%7c1&No=30&props=15292&dims=530&As=&N=0+3+10500915');No參數每頁增加15,但props讓我猜測。儘管如此,我建議不要進入這個網站,而只是使用硒與客戶端進行交互。這對於他們的變化也更加穩健。

+0

大尖,似乎做什麼我想它做的事。總之,有一個問題你可以幫我 – steph 2015-04-01 10:19:36

+0

這個問題會是什麼? – Joost 2015-04-01 10:21:13

+0

對不起,我被困在互聯網連接緩慢,所以我經常按下按鈕;) 你可以找到編輯 – steph 2015-04-01 10:30:19

1

我試過下面的代碼,能夠加載下一頁。希望這也能幫助你。 代碼:

from selenium import webdriver 
import os 
chromedriver = "C:\Users\pappuj\Downloads\chromedriver" 
os.environ["webdriver.chrome.driver"] = chromedriver 
driver = webdriver.Chrome(chromedriver) 
url='http://www.zoover.nl/cyprus' 
driver.get(url) 
driver.find_element_by_class_name('next').click() 

感謝

+0

這與原始問題有關嗎? – JabberwockyDecompiler 2015-05-14 19:04:03