【问题标题】:Handling data in inconsistent order for web scraping with Selenium使用 Selenium 以不一致的顺序处理数据以进行 Web 抓取
【发布时间】:2020-07-04 16:16:29
【问题描述】:

以下三个 URL 是我尝试抓取的数据示例。该信息位于页面左侧,包括运动信息以及其他一些统计数据。数据被作为一个大元素提取。我试图通过索引号分隔个人信息,但每个运动员的信息顺序不同,或者根本不可用。这会导致索引错误或一起获取错误信息(即在深蹲数下获得 40 码短跑):

  1. https://www.hudl.com/profile/7670389/GaQuincy-McKinstry 球衣号码:1 位置:CB,WR 身高和体重:6'1" 189lbs 40码短跑:4.55 替补席:190 深蹲(LBS):370 清洁(磅):225 班级:2021
  2. https://www.hudl.com/profile/10316846/Dylan-Rosiek 球衣号码:6 职位:美国职业棒球大联盟,RB 身高和体重:6'1" 210lbs 班级:2021
  3. https://www.hudl.com/profile/10015742/Donovan-Jackson 球衣号码:77 位置:T,G 身高和体重:6'4" 310lbs 40码短跑:5.1 垂直:29 强力球:35 替补席:365 深蹲(LBS):415 硬拉(LBS):435 班级:2021

如何确保我正在写入我的 pandas 数据库中的正确列。下面是我尝试为专门为该页面编制索引的第一个 URL 的代码,但不适用于其他页面。我暂时放了打印功能来看看我在拉什么数据,但最终会制作一个熊猫数据库。我也不确定是否应该通过 CSS 选择器或类名获取信息。

from selenium import webdriver
from selenium.common.exceptions import TimeoutException
import time

TIMEOUT = 5
driver = webdriver.Firefox()
driver.set_page_load_timeout(TIMEOUT)

url = 'https://www.hudl.com/profile/7670389/GaQuincy-McKinstry'
try:
    driver.get(url)
except TimeoutException:
    pass

time.sleep(3)

try:
    isPresent = driver.find_element_by_xpath('//[@id="app"]/div/div[2]/div/div/div[2]/div[3]/div/div[1]/div[1]/div[1]/button')
    isPresent.click()
except:
    pass

time.sleep(3)

skills = driver.find_elements_by_css_selector('#app > div > div.prof-flex-height > div > div > div.parallax-layer.front > div.profile-tab > div > div.left-column > div.stats > ul')
skills = [one.text for one in skills]
print(skills)

try:
    athletic_skills = driver.find_elements_by_class_name('stats-list')
    athletic_skills = [skill.text for skill in athletic_skills]
    athletic_skills = athletic_skills[-1].split('\n')
    jersey = athletic_skills[0].replace('Jersey #: ', '')
    position = athletic_skills[1].replace('Positions: ', '')
    height_weight = athletic_skills[2].replace('Height & Weight: ', '')
    height_weight = height_weight.split()
    height = height_weight[0]
    weight = height_weight[-1]
    yard_dash = athletic_skills[3].replace('40 Yard Dash: ', '')
    bench = athletic_skills[4].replace('Bench: ', '')
    squat = athletic_skills[5].replace('Squat(LBS): ', '')
    clean = athletic_skills[6].replace('Clean(LBS): ', '')
    grad_year = athletic_skills[7].replace('Class of: ', '')

    print(athletic_skills)
    print(jersey)
    print(position)
    print(height_weight)
    print(height)
    print(weight)
    print(yard_dash)
    print(bench)
    print(squat)
    print(clean)
    print(grad_year)
except:
    pass

driver.close()

【问题讨论】:

    标签: python selenium web-scraping


    【解决方案1】:

    简答:首先为每个玩家将原始数据加载到 Python 字典中。

    更长的答案:

    字典结构允许您将键(例如40 Yard Dash)映射到相关的统计信息(例如4.55)。

    您可以使用您已经在athletic_skills 中捕获的数据作为您的起点。

    例如:

    # new empty dictionary:
    mckinstry_skills = {}
    
    for skill_stats in athletic_skills:
        # separate the skill name from the related statistic:
        skill_stats = skill_stats.split(': ', 1) 
        # add this as a new entry into the dictionary:
        mckinstry_skills[skill_stats[0]] = skill_stats[1]
    
    # print the full dictionary:
    print(mckinstry_skills)
    
    # print the results of retrieving one item:
    print(mckinstry_skills['40 Yard Dash']) 
    

    第一个print 语句给出了这个输出(为了清楚起见,我格式化了):

    { 
      'Jersey #'       : '1', 
      'Positions'      : 'CB, WR', 
      'Height & Weight': '6\'1" 189lbs', 
      '40 Yard Dash'   : '4.55', 
      'Bench'          : '190', 
      'Squat(LBS)'     : '370', 
      'Clean(LBS)'     : '225', 
      'Class of'       : '2021'
    }
    

    第二个print 语句简单地返回:

    4.55
    

    现在,您始终可以可靠地获得所需 pandas 列的正确统计数据。

    由于并非所有玩家都拥有所有统计数据,因此您可能需要在尝试获取相关统计数据之前确保密钥存在:

    if '40 Yard Dash' in mckinstry_skills:
        print(mckinstry_skills['40 Yard Dash'])
    

    如果您不熟悉 dicts,这里有很多可用的概述。如果您已经很熟悉,请原谅我的过度解释。

    【讨论】:

      猜你喜欢
      • 2022-07-22
      • 2021-11-20
      • 1970-01-01
      • 2021-08-03
      • 2020-11-23
      • 1970-01-01
      • 2017-05-12
      • 1970-01-01
      • 2010-11-27
      相关资源
      最近更新 更多