【问题标题】:Create a loop for web scraping with selenium in python在 python 中使用 selenium 创建一个用于网络抓取的循环
【发布时间】:2019-03-15 22:55:40
【问题描述】:

我想创建一个循环,以便从 at the races 网站上抓取所有八场比赛中每匹马的个人时间数据。

以下是八场比赛的第一场比赛(17:15)的示例:

from selenium import webdriver

from selenium.webdriver.common.by import By

from selenium.webdriver.support import expected_conditions

from selenium.webdriver.support.ui import WebDriverWait

url = 'http://www.attheraces.com/racecard/Wolverhampton/6-October-2018/1715'

driver = webdriver.Chrome()

driver.get(url)

driver.implicitly_wait(2)

driver.find_element_by_xpath('//*[@id="racecard-tabs 1061960"]/div[1]/div/div[1]/ul/li[2]/a').click()

WebDriverWait(driver, 5).until(expected_conditions.presence_of_element_located((By.XPATH, '//*[@id="tab-racecard-sectional-times"]/div/div[1]/div[1]/div[2]/div/button')))

下一场比赛 (17:45) 的网址如下:

url = 'http://www.attheraces.com/racecard/Wolverhampton/6-October-2018/1745'

并且下面代码中的id随着url不断变化

driver.find_element_by_xpath('//*[@id="racecard-tabs 1061961"]/div[1]/div/div[1]/ul/li[2]/a').click()

所以对于 17:15,racecard-tabs 变为 1061960

对于 17:45,racecard-tabs 变为 1061961

18:15,raecard-tabs 变为 1061963,以此类推。

非常感谢任何帮助或建议。

【问题讨论】:

    标签: python html loops selenium web-scraping


    【解决方案1】:

    这会奏效。您甚至可以更改日期,其余的将自动为您执行!

    from selenium import webdriver
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support import expected_conditions
    from selenium.webdriver.support.ui import WebDriverWait
    
    def races(main_url):
        driver = webdriver.Chrome()
        driver.get(main_url)
        driver.implicitly_wait(2)
    
        races = driver.find_elements_by_class_name('time-location')
        races = [race.text[:5] for race in races]
        races = [race.replace(':', '') for race in races]
    
        driver.close()
    
        return races
    
    def scrape(url):
        driver = webdriver.Chrome()
        driver.get(url)
        driver.implicitly_wait(2)
        driver.find_elements_by_class_name('racecard-ajax-link')[1].click()
    
        WebDriverWait(driver, 5).until(expected_conditions.presence_of_element_located((By.XPATH, '//*[@id="tab-racecard-sectional-times"]/div/div[1]/div[1]/div[2]/div/button')))
    
        for horse in driver.find_elements_by_class_name('card-item'):
            horseName = horse.find_element_by_class_name('form-link').text
            times = horse.find_elements_by_class_name('sectionals-time')
            times = [time.text for time in times]
            print('{}: {}'.format(horseName, times))
        print()
    
        driver.close()
    
    def main():
        date = '6-October-2018'
        main_url = 'http://www.attheraces.com/racecard/Wolverhampton/' + date
        for race in races(main_url):
            url = main_url + '/' + race
            print(url)
            scrape(url)
    
    if __name__ == '__main__':
        main()
    

    【讨论】:

    • 再次感谢您的工作!尽管部分代码不断抛出错误:races = driver.find_elements_by_class_name('time-location') Traceback (last recent call last): File "", line 1, in races = driver .find_elements_by_class_name('time-location') NameError: name 'driver' is not defined
    • 是的,我得到 SyntaxError: multiple statements found while compile a single statement。如果我尝试在导入后运行代码,即 from def races(main_url): 我得到 SyntaxError: invalid syntax 并在 'scrape(url): 代码行突出显示 'def'。
    • 代码真的很好。创建一个新的 python 文件并复制我的 EXACT 代码并运行它。你做错了什么
    • @BrianC 我怀疑从这个答案到您自己的终端的复制过程中发生了一些事情;制表符和空格可能会在某处互换。复制后请检查制表符和空格是否与此答案中的相同。
    • 不要将这样的大块代码复制到交互式外壳中,这些通常不会起作用。将其保存到 .py 文件并执行。
    猜你喜欢
    • 2020-06-24
    • 2016-02-19
    • 1970-01-01
    • 2021-08-11
    • 2020-09-04
    • 1970-01-01
    • 2022-01-24
    • 2022-11-09
    • 1970-01-01
    相关资源
    最近更新 更多