首页 > 解决方案 > 刮板无法突然运行并且显示驱动程序未定义

问题描述

我正在运行一个从Nowgoal获取数据的刮板 它运行良好,直到今天早上。我再次运行它,程序没有任何更改,它向我显示错误“未定义驱动程序”。但是,我已经定义如下:

options = webdriver.ChromeOptions()         #initialise webdriver options
driver = webdriver.Chrome(resource_path('./drivers/chromedriver.exe'),options=options)

我不确定到底是什么问题,错误指向我退出驱动程序的程序的最后一行,如下所示:

driver.quit()

它发生了几次,我关闭了所有的 IDLE 并再次打开它,它起作用了。但是现在,不管它给我带来了同样的错误。

任何人都可以建议吗?

提前致谢,

泽普

以下是详细代码,请在此处获取 Link_output 文件以获取主要 url。

#######Main 脚本从这里开始 - 打开存储 url 的文件 - 确保 Link_output 与正在运行的脚本位于同一文件夹中######

read_workbook = load_workbook(filename="Link_output.xlsx")
read_sheet = read_workbook.active


for row in range(2,read_sheet.max_row+1):
    for column in "BDG":  #Here you can add or reduce the columns
        cell_name = "{}{}".format(column, row)
        main_urls.append(read_sheet[cell_name].value)

#we have URL ready
print('Urls we are going to scrape : ' ,main_urls)

#filter out the dictionary based on bookmakers entered in config file- we will have the bookmakers available in my_dictionay_button
wanted_bookmaker_lst = bkmaker_param.split(',')
for maker in wanted_bookmaker_lst:
    for k,v in main_dictionary_button.items():
        if  k.startswith(maker):my_dictionary_button[k]=v


#now loop through each URL
for file_key ,main_url in enumerate(main_urls):
    #start the new workbook for each new URL
    workbook = Workbook()
    #Error flag clear - first time action flag also cleared 
    i=0
    error_flag =0
    file_key += 1
    #Main url -print here-every third value is the link
    if file_key % 3 == 0:
        print(main_url)
        # first we will enter into main_url - urls generally open with Crown tab - so we will click through each button of the bookmakers
        
        for bookmaker ,odds_url_button  in my_dictionary_button.items():
            
            if i == 0 and error_flag ==  0  :#first time action
                #start the driver for the first time
                driver = webdriver.Chrome(resource_path('./drivers/chromedriver.exe'),options=options)
                #driver = webdriver.Chrome(executable_path = driver_path,options=options )
                try:
                    driver.get(main_url) #Get the main url
                except TimeoutException:
                    driver.get(main_url) #in case of timeout error - try again
                    time.sleep(5)
                try:
                    driver.find_element_by_xpath(odds_url_button).click() #click on the first bookmaker button
                    driver.switch_to.window(driver.window_handles[0])     #in case any pop up is opening due to any reason - swtich to main window
                except NoSuchElementException:                            #In case button is not found  
                    print('Button not found')
                    lst_of_reattempt.append(driver.current_url)           #Get the current url for which we were not able to find the button
                    saved_button_for_halftime = odds_url_button           #save the button for later reattempt
                    driver.quit()
                    i+=1                                                  #Firt time actions are over
                    error_flag ==  1                                      #initialise the error count
                    continue
                i+=1
            elif error_flag ==  1:                                        #if previous one went into error
                if odds_url_button == '//*[@id="htBtn"]/a':               #In case the error happened while clicking on half time button
                    half_time = 1
                    revised_url  = get_url_from_button(saved_button_for_halftime,main_url,half_time)# Get the revised url   
                    userAgent = ua.random                                                           #change user agent everytime browser went into error 
                    options.add_argument(f'user-agent={userAgent}')
                    driver = webdriver.Chrome(resource_path('./drivers/chromedriver.exe'),options=options) #trigger driver
                    #driver = webdriver.Chrome(executable_path = driver_path,options=options )
                    try:
                        driver.get(revised_url)                             #Revised URL open
                        time.sleep(5)
                    except TimeoutException:
                        driver.get(revised_url)                             #In case of timeout- reattempt
                        time.sleep(5)
                    error_flag = 0                                          #disable error flag - so we can proceed as usual
                else:
                    revised_url  = get_url_from_button(odds_url_button,main_url)
                    userAgent = ua.random
                    options.add_argument(f'user-agent={userAgent}')
                    driver = webdriver.Chrome(resource_path('./drivers/chromedriver.exe'),options=options)
                    #driver = webdriver.Chrome(executable_path = driver_path,options=options )
                    try:
                        driver.get(revised_url)
                    except TimeoutException:
                        driver.get(revised_url)
                    error_flag = 0                
            else:                                                    #In case of no error
                driver.find_element_by_xpath(odds_url_button).click()#Click on next button
                driver.switch_to.window(driver.window_handles[0])    #in case any pop up is opening due to any reason - swtich to main window
                i+=1
            time.sleep(random.randint(5,7))                         #sleep for random amount of time - to make the script robust

            htmlSource = driver.page_source                         #Get the html code
            soup = bs4.BeautifulSoup(htmlSource,'html.parser')      #pass the page

            #get the fixed data which is common and do not change for one book maker
            title, home_team , away_team , place, weather ,tournament,m_date,m_time,data_first,data_second,data_third,final_score = get_fixed_data(soup)
            #home team ranking
            home_team_ranking  = main_urls[file_key-3]
            away_team_ranking  = main_urls[file_key-2]
            print('Title data :',title)
            if title != 'No Data':#check if the data found or not
                #create the folder path
                print(m_date)
                folder_month ,folder_day ,folder_year = m_date.split('-') #/
                folder_hour ,folder_minute = m_time.split(':')
                
                #fle_name = folder_day +folder_month + folder_year
                #folder_name = folder_day +'_'+folder_month+'_' + folder_year

                #convert the time to gmt
                folder_time_string =  folder_year +'-'+folder_month +'-'+folder_day +' '+ folder_hour+':'+folder_minute+':00'
                #folder name change
                folder_name =time.strftime("%d-%m-%Y", time.gmtime(time.mktime(time.strptime(folder_time_string, "%Y-%d-%m %H:%M:%S"))))

                print(bookmaker)
                #Output_file_format
                try:
                    print('Creating directory')
                    os.mkdir(os.path.join(os.getcwd()+'\\'+folder_name))
                except FileExistsError:
                    print('Directory already exist')                    
                
                inter_file_name = 'Odds_nowgoal_'+str(title.replace('v/s','vs'))+'_'+folder_name+'.xlsx'
                ola = os.path.join('\\'+folder_name,inter_file_name)
                output_file_name = os.path.join(os.getcwd()+ola)
                #sheet_title_first_table
                sheet_title =  '1X2 Odds_'+bookmaker
                
                #add data to excel
                excel_add_table(sheet_title,data_first,title,home_team , away_team , place, weather ,tournament,m_date,m_time,bookmaker,home_team_ranking,away_team_ranking,final_score)

                #sheet_title_second_table
                sheet_title =  'Handicap Odds_'+bookmaker
                
                #add data to excel
                excel_add_table(sheet_title,data_second,title,home_team , away_team , place, weather ,tournament,m_date,m_time,bookmaker,home_team_ranking,away_team_ranking,final_score)

                #sheet_title_third_table
                sheet_title =  'Over_Under Odds_'+bookmaker
                
                #add data to excel
                excel_add_table(sheet_title,data_third,title,home_team , away_team , place, weather ,tournament,m_date,m_time,bookmaker,home_team_ranking,away_team_ranking,final_score)
            else :
                lst_of_reattempt.append(home_team_ranking)
                lst_of_reattempt.append(away_team_ranking)
                lst_of_reattempt.append(driver.current_url)     #add the url into list of reattempt
                saved_button_for_halftime = odds_url_button     #save the button when error happens - so we can convert it into URL and later reattempt
                error_flag = 1
                driver.quit()                                   #Quit the driver in case of any error 
            
        driver.quit()

标签: python-3.xselenium-webdriverweb-scrapingbeautifulsoupselenium-chromedriver

解决方案


推荐阅读