【问题标题】:Using Selenium and BeautifulSoup Specific table is not returning by providing table ID Python3.7 ( returns Empty Array)使用 Selenium 和 BeautifulSoup 通过提供表 ID Python3.7 不会返回特定表(返回空数组)
【发布时间】:2019-03-06 01:12:20
【问题描述】:

我正在尝试使用 BeautifulSoup4 和 Selenium 从网页中获取特定表,该表存在于那里,但没有使用此代码获取。 我试过这个

from bs4 import BeautifulSoup
from selenium import webdriver
import time

driver = webdriver.Chrome()
url = 'https://de.sharkscope.com/#Find-Tournament/networks/PokerStars/tournaments/2412466083'
driver.get(url)
res = driver.execute_script("return document.documentElement.outerHTML")
time.sleep(5)
driver.quit()

soup = BeautifulSoup(res, 'lxml')
targetTable = soup.findAll('table', {'id': 'tournamentgrid-1538318460041'})
print(targetTable)

screenshot table With ID 和结果 DisplayResult

平台和工具

  • Microsoft Windows 10 x64
  • 使用 Chromium Webdriver(已安装 32 位版本)

已编辑。我已经试过了

table = driver.get_element_by_id('tournamentgrid-1538318460041')

&

table = driver.get_element_by_xpath('//*[@id="tournamentgrid-1538398792982"]')

【问题讨论】:

    标签: python selenium selenium-webdriver beautifulsoup dynamic-tables


    【解决方案1】:

    您可以尝试使用其 xpath 访问表

    driver.find_element_by_xpath('xpath_here')
    

    或者您可以通过

    获取所有表格行(所有<tr>元素对象)
    driver.find_elements_by_xpath('xpath_here') 
    

    并遍历行元素列表提取所需数据,如使用.text提取文本 访问 https://selenium-python.readthedocs.io/api.html了解更多详情

    【讨论】:

    • 我尝试使用 find_element_by_id() ** 和 ** find_element_by_xpath() 根本不起作用
    猜你喜欢
    • 2021-10-27
    • 2019-04-18
    • 1970-01-01
    • 2017-04-24
    • 2021-02-20
    • 2019-12-12
    • 2019-03-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多