使用Chrome无头浏览器下载CSV

时间:2018-06-05 18:26:04

标签: python google-chrome google-chrome-headless

我一直在使用这个脚本遇到麻烦,似乎无法确定问题所在。我试图做的就是自动化文件下载。什么是奇怪的有时它是有效的,有时它会在启动DevTools后挂起。

这是相关部分:

def enable_download_in_headless_chrome(browser, download_dir):
    #add missing support for chrome "send_command"  to selenium webdriver
    browser.command_executor._commands["send_command"] = ("POST", '/session/$sessionId/chromium/send_command')

    params = {'cmd': 'Page.setDownloadBehavior', 'params': {'behavior': 'allow', 'downloadPath': download_dir}}
    browser.execute("send_command", params)

chrome_options = Options()
chrome_options.add_argument("--headless")
browser = webdriver.Chrome(chrome_options=chrome_options, executable_path=r'C:\Windows\chromedriver.exe')
browser.set_window_size(1024, 768)

enable_download_in_headless_chrome(browser, r"C:\Users\Nick\Downloads")

browser.get(bs_url_1.format(season=season,player_type=player_type,pitcher_throws=pitcher_throws,batter_stands=batter_stands))
wait = WebDriverWait(browser, 5)
wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, "#table_all_pid_"))).click()  

while not os.path.exists(file_path):
    time.sleep(1)

if os.path.isfile(file_path):           
    df_savant = pd.read_csv("C:/Users/Nick/Downloads/savant_data.csv", usecols=[0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23])
    df_savant.rename(columns = {'player_id':'mlb_id'}, inplace=True)
    df_savant['mlb_id'] = df_savant['mlb_id'].astype(int)
    browser.quit()

os.remove("C:/Users/Nicholas Wolf/Downloads/savant_data.csv")

我尝试了多种执行“点击”的变体。动作(下面)并始终得到相同的结果。有时它会起作用,有时它会挂起。

browser.find_element_by_id("table_all_pid_").click() 

如果有人对我为什么要挣扎这个问题有所了解,我会很感激。

0 个答案:

没有答案