【问题标题】:Web Scraping Jquery tables with BeautifulSoup and Selenium使用 BeautifulSoup 和 Selenium 抓取 Jquery 表
【发布时间】:2020-03-25 00:13:02
【问题描述】:

我正在尝试从网站中的表中获取数据,该网站每天都会从该网站更新 http://mananciais.sabesp.com.br/HistoricoSistemas?SistemaId=0.

我正在学习 BeautifulSoup 和 Selenium,并尝试使用这些包访问数据。

但是,网站的源代码并没有透露表格中的数据。据我了解,该网站是使用 jQuery Grid 构建的。

到目前为止,我只做了这个,尽管我已经尝试了几件事。

import os
import time
from selenium import webdriver

# Escolhe o driver
driver = webdriver.Firefox()

# Acessa o site
site = 'http://mananciais.sabesp.com.br/HistoricoSistemas'
driver.get(site)

soup = BeautifulSoup(driver.page_source, 'html.parser')

print(soup.prettify())

如何访问这些数据? 我想用这个来分析

【问题讨论】:

    标签: python selenium beautifulsoup


    【解决方案1】:

    数据由Java Scripts渲染。您需要等待页面正确加载才能获取page_source。

    诱导WebDriverWait()并等待visibility_of_element_located()

    from selenium import webdriver
    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.support import expected_conditions as EC
    from selenium.webdriver.common.by import By
    # Escolhe o driver
    
    driver = webdriver.Firefox()
    # Acessa o site
    site = 'http://mananciais.sabesp.com.br/HistoricoSistemas'
    driver.get(site)
    WebDriverWait(driver,10).until(EC.visibility_of_element_located((By.ID,"contenttabledivjqxGrid")))
    soup = BeautifulSoup(driver.page_source, 'html.parser')
    print(soup.prettify()) 
    

    希望这会有所帮助。

    【讨论】:

    • 太棒了!改善!它有效,但只返回表格的可见部分......用户必须向下滚动并向右滚动 BeautifulSoup 的部分没有得到。谢谢@KunduK!我会继续学习的!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-09-13
    • 2021-10-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-08-14
    • 2021-06-23
    相关资源
    最近更新 更多