【问题标题】:Extract a specific element of a table with selenium web driver使用 selenium web 驱动程序提取表的特定元素
【发布时间】:2020-09-08 16:14:33
【问题描述】:

您好,我正在尝试从该网站提取一些元素:https://www.oddsportal.com/basketball/italy/lega-a-super-cup/sassari-brindisi-rTJFaIyk/

我希望主客队的赔率最高。这些数据位于表格末尾,分别是:1.31 和 4.57

这是我的脚本:

#!/usr/bin/python3
# -*- coding: utf­-8 ­-*-

from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from concurrent.futures import ThreadPoolExecutor

options = Options()
options.headless = True
options.add_argument("window-size=1400,800")
options.add_argument("--no-sandbox")
options.add_argument("--disable-gpu")
options.add_argument("start-maximized")
options.add_argument("enable-automation")
options.add_argument("--disable-infobars")
options.add_argument("--disable-dev-shm-usage")

driver = webdriver.Chrome(options=options)

driver.get("https://www.oddsportal.com/basketball/italy/lega-a-super-cup/sassari-brindisi-rTJFaIyk/")

home_average_odds = [my_elem.text for my_elem in WebDriverWait(driver, 10).until(EC.presence_of_all_elements_located((By.XPATH, '//*[@class="highest"]/td[contains(@class, "right")]')))]

for i in home_average_odds:
    print(i)

driver.close()
driver.quit()        

问题是我没有好的结果,这是输出:

1.31
4.30
 100.4%

【问题讨论】:

    标签: python-3.x selenium-webdriver data-extraction


    【解决方案1】:

    什么是“好结果”?

    您可以通过用 pandas 拉表并拉出该行来获得平均值:

    from selenium import webdriver
    from selenium.webdriver.chrome.options import Options
    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support import expected_conditions as EC
    from concurrent.futures import ThreadPoolExecutor
    
    import pandas as pd
    
    options = Options()
    options.headless = True
    options.add_argument("window-size=1400,800")
    options.add_argument("--no-sandbox")
    options.add_argument("--disable-gpu")
    options.add_argument("start-maximized")
    options.add_argument("enable-automation")
    options.add_argument("--disable-infobars")
    options.add_argument("--disable-dev-shm-usage")
    
    driver = webdriver.Chrome(options=options)
    
    driver.get("https://www.oddsportal.com/basketball/italy/lega-a-super-cup/sassari-brindisi-rTJFaIyk/")
    html = driver.page_source
    
    df = pd.read_html(html)[0]
    
    avg = df[df['Bookmakers'] == 'Average']
    print (avg)
    

    输出:

    print (avg)
       Bookmakers     1     2 Payout Unnamed: 4
    49    Average  -408  +291  94.4%        NaN
    

    输出与表格匹配

    【讨论】:

    • 感谢您的回归。但我有完全相同的问题。这些不是相同的元素。在“平均”行的网页中,我们有:1,24 3,92 94,1%,但在您的输出中,我们得到 1,24 3,91 94,4%
    • 网页中最后一行“最高”的问题相同,我们有 1,31 4,57 101,8 而在使用您的方法的输出中,我们得到 1,31 4,3 100,4
    • 在我的情况下,最后一行“最高”的“好结果”将是 1,31 4,57 101,8%
    • 我正在查看网页和输出,它们完全一样。你需要更清楚地解释什么问题呢。转到您的帖子并更清楚地解释问题。
    • 对不起,这是个人错误。我以与您相反的方式登录了我的脚本。所以在我的情况下,我有更多的博彩公司可用,因此平均和最高的值并不完全相同。
    猜你喜欢
    • 2015-09-19
    • 2020-12-29
    • 1970-01-01
    • 2019-04-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-09-28
    相关资源
    最近更新 更多