【问题标题】:Selenium- page source not changing after performing click()Selenium-页面源在执行 click() 后没有改变
【发布时间】:2018-10-30 05:42:19
【问题描述】:

我正在抓取这个网站:https://www.findinall.com/finance-category-396

我正在使用 Selenium(Python) 并在名为“Showing”的下拉菜单中的选项 300 上执行单击选项。点击自动成功,网页将12个页面的全部数据集中显示在一个页面中,但是数据抓取后,只获取到前12个数据项,即page_source没有变化。

这是我的代码:

from selenium import webdriver
driver=webdriver.Chrome("/home/ronith/Downloads/chromedriver")

driver.get('https://www.findinall.com/finance-category-396/#')
driver.find_element_by_xpath("//select[@name='per_page']/option[@value  
='300']").click()
driver.implicitly_wait(5)
data=driver.find_elements_by_xpath('//div[@class="pro-list-tb mt15"]')
for i in range(len(data)):
   print(data[i].text,'\n\n')
driver.close()

我想抓取执行点击操作后可用的全部数据。我在这里做错了什么?

【问题讨论】:

  • 了解implicit waitexplicit wait 之间的区别将帮助您解决问题。在页面有机会刷新所有 300 条记录之前,您正在填充您的 data 列表。 selenium-python.readthedocs.io/waits.html

标签: python selenium web-scraping selenium-chromedriver


【解决方案1】:

而不是使用 Selenium 来抓取使用 BeautifulSoup,请求并导入以下代码

import sys
from PyQt5.QtWidgets import QApplication
from PyQt5.QtCore import QUrl
from PyQt5.QtWebEngineWidgets import QWebEnginePage
from bs4 import BeautifulSoup
import requests


class Page(QWebEnginePage):
    def __init__(self, url):
        self.app = QApplication(sys.argv)
        QWebEnginePage.__init__(self)
        self.html = ''
        self.loadFinished.connect(self._on_load_finished)
        self.load(QUrl(url))
        self.app.exec_()

    def _on_load_finished(self):
        self.html = self.toHtml(self.Callable)

    def Callable(self, html_str):
        self.html = html_str
        self.app.quit()

并从 jssoup 导入页面(我将其命名为“jssoup”)将此代码导入另一个类似这样的 python 文件中

page = Page(url)
soup = bs.BeautifulSoup(page.html, 'lxml')
js_test = soup.find('p', class_='jstest')
print(js_test)

【讨论】:

  • 我正在使用 Selenium 自动单击按钮,否则我只会使用 BeautifulSoup
猜你喜欢
  • 2015-06-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-01-16
  • 1970-01-01
  • 2021-10-19
  • 2017-09-18
  • 2018-11-04
相关资源
最近更新 更多