用beautifulsoup-python调用onclick事件

invoking onclick event with beautifulsoup python

本文关键字:事件 onclick 调用 beautifulsoup-python      更新时间:2023-09-26

我正试图从这个网站获取塞浦路斯所有住宿的链接:http://www.zoover.nl/cyprus

到目前为止,我可以检索到已经显示的前15个。所以现在我必须调用"volgende"链接上的点击。然而,我不知道如何做到这一点,在源代码中,我无法追踪被调用的函数,例如,这里发布的类似内容:调用";点击事件";在html页面上使用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.com.mon.exceptions.StaleElementReferenceException:消息:元素不再附加到DOMStacktrace:在fxdriver.cache.getElementAt(resource://fxdriver/modules/web-element-cache.js:8956)在Utils.getElementAt(file:///var/folders/n4/fhvhqlmx23s8ppxbrxrpws3c0000gn/T/tmpKFL43_/extensions/fxdriver@googlecode.com/components/command processor.js:8546)在fxdriver.seconditions.visible(file:///var/folders/n4/fhvhqlmx23s8ppxbrxrpws3c0000gn/T/tmpKFL43_/extensions/fxdriver@googlecode.com/components/command processor.js:9585)在DelayedCommand.prototype.checkPreconditions(file:///var/folders/n4/fhvhqlmx23s8ppxbrxrpws3c0000gn/T/tmpKFL43_/extensions/fxdriver@googlecode.com/components/command processor.js:12257)在DelayedCommand.prototype.executeInternal/h(file:///var/folders/n4/fhvhqlmx23s8ppxbrxrpws3c0000gn/T/tmpKFL43_/extensions/fxdriver@googlecode.com/components/command processor.js:12274)在DelayedCommand.prototype.executeInternal(file:///var/folders/n4/fhvhqlmx23s8ppxbrxrpws3c0000gn/T/tmpKFL43_/extensions/fxdriver@googlecode.com/components/command processor.js:12279)在DelayedCommand.protype.execule/<(file:///var/folders/n4/fhvhqlmx23s8ppxbrxrpws3c0000gn/T/tmpKFL43_/extensions/fxdriver@googlecode.com/components/command processor.js:12221)

我很困惑:/

虽然使用Beautifulsoup的evaluateJavaScript方法可能很诱人,但最终Beautifuuloup是一个解析器,而不是一个交互式web浏览客户端。

你应该认真考虑用硒来解决这个问题,如这个答案所示。有相当好的Python绑定可用于selenium。

您可以使用selenium找到元素并单击它,然后将页面传递给Beautifulsoup,并使用现有代码获取链接。

或者,您可以使用onclick处理程序中列出的Javascript。我从来源EntityQuery('Ns=pPopularityScore%7c1&No=30&props=15292&dims=530&As=&N=0+3+10500915');中得到了这个。对于每一页,No参数以15递增,但props让我猜测。不过,我建议大家不要涉足这一领域,只需像客户一样使用硒元素与网站互动即可。这对他们这边的变化也更加有力。

我尝试了以下代码,并能够加载下一页。希望这对你也有帮助。代码:

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()

感谢