【问题标题】:Selenium Scraping Javascript TableSelenium 抓取 Javascript 表
【发布时间】:2019-04-13 19:55:07
【问题描述】:

我正在努力按照下面的代码进行抓取。如果有人可以看看我错过了什么,会很高兴吗? 问候 PyProg70

from selenium import webdriver
from selenium.webdriver import FirefoxOptions
from selenium.webdriver.firefox.firefox_binary import FirefoxBinary
from bs4 import BeautifulSoup
import pandas as pd
import re, time

binary = FirefoxBinary('/usr/bin/firefox')
opts = FirefoxOptions()
opts.add_argument("--headless")

browser = webdriver.Firefox(options=opts, firefox_binary=binary)
browser.implicitly_wait(10)

url = 'http://tenderbulletin.eskom.co.za/'
browser.get(url)

html = browser.page_source
soup = BeautifulSoup(html, 'lxml')

print(soup.prettify())

【问题讨论】:

    标签: python selenium


    【解决方案1】:

    不是Java,而是Javascript。它是动态页面,您需要等待并检查 Ajax 是否完成了使用 WebDriverWait 呈现的请求和内容。

    ....
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support.ui import WebDriverWait 
    from selenium.webdriver.support import expected_conditions as EC
    
    .....
    browser.get(url)
    
    # wait max 30 second until table loaded
    WebDriverWait(browser, 30).until(EC.presence_of_element_located((By.CSS_SELECTOR , 'table.CSSTableGenerator .ng-binding')))
    
    html = browser.find_element_by_css_selector('table.CSSTableGenerator')
    soup = BeautifulSoup(html.get_attribute("outerHTML"), 'lxml')
    print(soup.prettify().encode('utf-8'))
    

    【讨论】:

      猜你喜欢
      • 2013-11-13
      • 1970-01-01
      • 2019-11-15
      • 2014-02-22
      • 1970-01-01
      • 2020-11-03
      • 2013-01-09
      • 2017-03-26
      • 2021-06-04
      相关资源
      最近更新 更多