如何根据HTML从“统计信息"页面中提取播放器的信息? [英] How to extract the player's information from the Statistics page as per the HTML?

查看:157
本文介绍了如何根据HTML从“统计信息"页面中提取播放器的信息?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用硒为网站抓取一些信息,以下是该网站的链接 http://www.ultimatetennisstatistics.com/playerProfile?playerId=4742 我要获取的信息在玩家统计信息"下,我的代码现在打开玩家的个人资料,然后打开玩家的统计信息页面.远

I am trying to scrape some information for a website using selenium below is the link to the website http://www.ultimatetennisstatistics.com/playerProfile?playerId=4742 the information i am trying to get is under the player 'statistics' my code right now opens the player's profile and then opens the player's statistics page i am trying to find a way to extract the information in the player's statistics page below is my code so far

from bs4 import BeautifulSoup
from selenium import webdriver

driver = webdriver.Chrome()
driver.get("http://www.ultimatetennisstatistics.com/playerProfile?playerId=4742")
soup = BeautifulSoup(driver.page_source,"lxml")
try:
dropdown = driver.find_element_by_xpath('//*[@id="playerPills"]/li[9]/a')
dropdown.click()


bm = driver.find_element_by_id('statisticsPill')
bm.click()

for i in soup.select('#statistics table.table tr'):
    print(i)
    data1 = [x.get_text(strip=True) for x in i.select("th,td")]
    print(data1)

except ValueError:
      print("error")

我 服务

I Serve

                            <th class="pct-data text-right"><i class="fa fa-percent"></i></th>
                            <th class="raw-data text-right" style="display: none;"><i class="fa fa-hashtag"></i></th>
                        </tr>
                        </thead>
                        <tbody>
                        <tr>
                            <td>Ace %</td>



                            <th class="text-right pct-data">23.4%</th>
                            <th class="raw-data text-right" style="display: none;">12942 / 55377</th>


                        </tr>
                        <tr>
                            <td>Double Fault %</td>



                            <th class="text-right pct-data">4.2%</th>
                            <th class="raw-data text-right" style="display: 

推荐答案

要从统计信息页面中提取玩家的信息,您可以使用以下解决方案:

To extract the information of the player's from the Statistics page you can use the following solution:

  • 代码块:

  • Code Block:

from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC

options = webdriver.ChromeOptions() 
options.add_argument("start-maximized")
options.add_argument('disable-infobars')
driver=webdriver.Chrome(chrome_options=options, executable_path=r'C:\Utility\BrowserDrivers\chromedriver.exe')
driver.get("http://www.ultimatetennisstatistics.com/playerProfile?playerId=4742")
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//ul[@id='playerPills']//a[@class='dropdown-toggle'][normalize-space()='Statistics']"))).click()
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//ul[@class='dropdown-menu']//a[@id='statisticsPill'][normalize-space()='Statistics']"))).click()
statistics_items = WebDriverWait(driver, 10).until(EC.visibility_of_any_elements_located((By.XPATH, "//table[@class='table table-condensed table-hover table-striped']//tbody//tr/td")))
statistics_value = WebDriverWait(driver, 10).until(EC.visibility_of_any_elements_located((By.XPATH, "//table[@class='table table-condensed table-hover table-striped']//tbody//tr//following::th[1]")))
for item, value in zip(statistics_items, statistics_value):
    print('{} {}'.format(item.text, value.text))

  • 控制台输出:

  • Console Output:

    Ace % 4.0%
    Double Fault % 2.1%
    1st Serve % 68.7%
    1st Serve Won % 71.8%
    2nd Serve Won % 57.3%
    Break Points Saved % 66.3%
    Service Points Won % 67.2%
    Service Games Won % 85.6%
    Ace Against % Return
    Double Fault Against % 7.2%
    1st Srv. Return Won % 3.4%
    2nd Srv. Return Won % 34.2%
    Break Points Won % 55.3%
    Return Points Won % 44.9%
    Return Games Won % 42.4%
    Points Dominance 33.3%
    Games Dominance Total
    Break Points Ratio 1.29
    Total Points Won % 2.31
    Games Won % 1.33
    Sets Won % 54.4%
    Matches Won % 59.7%
    Match Time 77.2%
    

  • 这篇关于如何根据HTML从“统计信息"页面中提取播放器的信息?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆