【问题标题】:Selenium+Python. Repeating parse operations on next page not working硒+蟒蛇。在下一页上重复解析操作不起作用
【发布时间】:2016-12-12 22:08:52
【问题描述】:

所以我想要完成的是使用 Beautiful Soup 解析和提取页面信息,然后使用 Selenium 导航到下一页,重复此操作,直到没有下一页。这是我遇到问题的相关代码sn-p

while True:
    try:
        for content in cards.find_all('div',{'class':'el-card-visual'}): 
            print (content.find('img')['alt'])
            print (content.find('a')['href']) 
        elm = driver.find_element_by_css_selector('span.is-next')
        elm.click()
        URL = driver.current_url 
        driver.get(URL)
        HTML = driver.page_source
        cards = BeautifulSoup(HTML,'lxml') 
    except:
        break

当我尝试运行它时,我得到了这个错误:

Traceback (most recent call last):
File "testpull.py", line 18, in <module>
elm.click()
File "C:\Program Files\Python35\lib\site-packages\selenium\webdriver\remote\webelement.py", line 72, in click
self._execute(Command.CLICK_ELEMENT)
File "C:\Program Files\Python35\lib\site-packages\selenium\webdriver\remote\webelement.py", line 461, in _execute
return self._parent.execute(command, params)
File "C:\Program Files\Python35\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 236, in execute
self.error_handler.check_response(response)
File "C:\Program Files\Python35\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 192, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.WebDriverException: Message: unknown error:  Element is not clickable at point (172, 612). Other element would receive the click: <p class="cc_message">...</p>
(Session info: chrome=54.0.2840.99)
(Driver info: chromedriver=2.24.417431  (9aea000394714d2fbb20850021f6204f2256b9cf),platform=Windows NT 10.0.10240 x86_64)

我尝试在 try 语句中没有最后一点的情况下运行它(elm.click() 之后的所有内容),但它不断循环并一遍又一遍地从第一页打印回内容。

对于这个问题的最佳解决方案有什么想法吗?

【问题讨论】:

  • 点击Next按钮后URL会发生变化吗?

标签: python python-3.x selenium beautifulsoup


【解决方案1】:

昨晚我自己弄乱了一些代码,发现最后我有太多多余的东西了。我所需要的只是:

elm = driver.find_element_by_css_selector('span.is-next')
elm.click()
HTML = driver.page_source
cards = BeautifulSoup(HTML,'lxml') 

没有别的了。

【讨论】:

    猜你喜欢
    • 2017-08-17
    • 2019-02-14
    • 2016-05-28
    • 2021-06-28
    • 2021-07-07
    • 2023-02-06
    • 1970-01-01
    • 1970-01-01
    • 2016-10-16
    相关资源
    最近更新 更多