Python中的多线程抓取

我试图从赛车网站获取一些实时计时数据,该网站会动态生成表格和地图。 我发现此代码可以正常工作,但每3-4秒只能更新一次。 我尝试如下进行多线程处理,但是由于没有变化,所以我一定做错了。 有人可以帮我更快地获取数据吗? 注意:我已经编码了整整六天了,请保持谨慎...

from selenium import webdriver
import time

import concurrent.futures

driverList = []
drivernums = ['01', '02', '03', '04', '05', '06', '07', '08', '09', '10', '11', '12', '13', '14', '15', '16', '17',
              '18', '19', '20']
for d in range(1, 22):
    driverList.append(
        {"Pos": 0, "Name": '', "Lap": 0, "Gap": 0, "Interval": 0, "Pits": 0, 'Tyres': 0, "Speed": 0, 'S1': 0, 'S2': 0,
         'S3': 0,
         'Laptime': 0})


driver = webdriver.Chrome("C:/Users/paulm/Desktop/chromedriver.exe")
driver.get("https://f1.tfeed.net/2017/silverstone")

def main():
    while True:
        t0 = time.time()

        with concurrent.futures.ThreadPoolExecutor(max_workers=20) as executor:
            executor.map(grabdata, drivernums)

        printdata(t0)


def printdata(t0):
    for d in drivernums:
        print(driverList[int(d)]["Pos"] + '\t' + driverList[int(d)]["Name"] + '\t' + driverList[int(d)][
            "Lap"] + '\t' +
              driverList[int(d)]["Gap"] + '\t' +
              driverList[int(d)]["Interval"] + '\t' + driverList[int(d)]["Pits"] + '\t' + driverList[int(d)][
                  "Tyres"] + '\t' + driverList[int(d)][
                  "Speed"] + '\t' + driverList[int(d)]["S1"] + '\t' + driverList[int(d)]["S2"] + '\t' +
              driverList[int(d)]["S3"] + '\t' + driverList[int(d)]["Laptime"])
    t1 = time.time()
    print(f"Print took {round(t1 - t0, 2)} seconds.")


def grabdata(d):

    if d == '10':
        sessiontimeremaining = driver.find_element_by_id('stats_si_time')
        safetycar = driver.find_element_by_id('stats_si_safetycar')
        racelaps = driver.find_element_by_id('stats_si_laps')
        print(str(sessiontimeremaining.text) + '\t\t' + str(safetycar.text) + '\t\t' + str(racelaps.text) + '\n')
    driverList[int(d)]['Pos'] = driver.find_element_by_id(f'i_{d}_pos').text
    driverList[int(d)]['Name'] = driver.find_element_by_id(f'i_{d}_nick').text[0:3].upper()
    driverList[int(d)]['Lap'] = driver.find_element_by_id(f'i_{d}_lap').text
    driverList[int(d)]['Gap'] = driver.find_element_by_id(f'i_{d}_gap').text
    driverList[int(d)]['Interval'] = driver.find_element_by_id(f'i_{d}_int').text[:-1]
    driverList[int(d)]['Pits'] = driver.find_element_by_id(f'i_{d}_pits').text
    driverList[int(d)]['Tyres'] = driver.find_element_by_id(f'i_{d}_tyres').text
    driverList[int(d)]['Speed'] = driver.find_element_by_id(f'i_{d}_speed').text
    driverList[int(d)]['S1'] = driver.find_element_by_id(f'i_{d}_s1').text
    driverList[int(d)]['S2'] = driver.find_element_by_id(f'i_{d}_s2').text
    driverList[int(d)]['S3'] = driver.find_element_by_id(f'i_{d}_s3').text
    driverList[int(d)]['Laptime'] = driver.find_element_by_id(f'i_{d}_t').text
评论