2015-04-01 140 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