使用 beautifulsoup python 调用 onclick 事件

Posted

技术标签:

【中文标题】使用 beautifulsoup python 调用 onclick 事件【英文标题】:invoking onclick event with beautifulsoup python 【发布时间】:2015-06-05 18:25:03 【问题描述】:

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

到目前为止,我可以检索已经显示的前 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 堆栈跟踪: 在 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.preconditions.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.prototype.execute/

我很困惑:/

【问题讨论】:

【参考方案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()

谢谢

【讨论】:

这和原来的问题有关系吗? 只有在按钮点击后才能得到soap结果,你可以使用以下代码:soup_level2 = BeautifulSoup(driver.page_source, 'html.parser')【参考方案2】:

虽然尝试使用 Beautifulsoup 的 evaluatejavascript 方法执行此操作可能很诱人,但最终,Beautifulsoup 是一个 parser 而不是交互式网络浏览客户端。

您应该认真考虑使用 selenium 解决此问题,如 this answer 中的简要说明。有相当不错的 Python bindings 可用于 selenium。

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

或者,您可以使用 onclick 处理程序中列出的 Javascript。我从源代码中提取了这个:EntityQuery('Ns=pPopularityScore%7c1&No=30&props=15292&dims=530&As=&N=0+3+10500915');No 参数每页增加 15,但 props 让我猜测。不过,我建议不要参与其中,而只是像客户一样使用 selenium 与网站进行交互。这对他们方面的变化也更加稳健。

【讨论】:

很棒的提示,它似乎可以满足我的要求。无论如何,有一个问题你可以帮我解决 那会是什么问题? 对不起,我的网速太慢了,所以我按得太频繁了;)你可以在 EDIT 下找到问题 一般来说,每个 SO 问题最好坚持一个主题。无论如何,您似乎在更改页面后使用的是 parsedZooverWeb 的旧状态。相反,将对find_all 的调用替换为另一个driver.find_element_by_class_name 非常感谢!抱歉混淆了问题!

以上是关于使用 beautifulsoup python 调用 onclick 事件的主要内容,如果未能解决你的问题,请参考以下文章

Python BeautifulSoup库使用

python爬虫之beautifulsoup的使用

python BeautifulSoup的简单使用

python中的BeautifulSoup使用小结

python3.4 使用BeautifulSoup

python爬虫——BeautifulSoup