gpt4 book ai didi

python - 使用 Selenium 和 BeautifulSoup 通过提供表 ID Python3.7 不会返回特定表(返回空数组)

转载 作者:太空宇宙 更新时间:2023-11-03 21:44:43 25 4
gpt4 key购买 nike

我正在尝试使用 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

平台和工具

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

已编辑。我已经尝试过

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

&

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

最佳答案

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

driver.find_element_by_xpath('xpath_here')

或者您可以通过

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

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

关于python - 使用 Selenium 和 BeautifulSoup 通过提供表 ID Python3.7 不会返回特定表(返回空数组),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52582418/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com