[問題] selenium headless send_Keys

看板Python作者 (酷扇)時間5年前 (2019/09/08 19:38), 編輯推噓1(103)
留言4則, 2人參與, 5年前最新討論串1/1
各位版友好, 小弟爬蟲學了一段時間,但這個問題卡了很久.. 我的目標是:我想利用selenium + chromedriver爬取匯率網頁,並且透過send_Keys 去改變日期,得到該天的匯率資料。 舉例來說,我輸入2019-09-06,USD-EUR的平均賣出價是0.90588 如果沒有headless時,send_Keys是正常的,selenium可以抓到0.90588 但如果有headless的情況時,send_Keys無法運作,date不會變,只能抓到今日的0.90721 因為未來程式會放在linux server上,一定得要加上headless, 但此時send_Keys出了問題,不知道是否有解?先謝謝各位的幫忙,thanks!! 程式碼如下: from bs4 import BeautifulSoup from selenium import webdriver import time import pandas as pd from selenium.webdriver.common.keys import Keys chrome_options = webdriver.ChromeOptions() chrome_options.add_argument('--no-sandbox') #chrome_options.add_argument('--headless') chrome_options.add_argument('--disable-dev-shm-usage') chrome_options.add_argument('--disable-extensions') chrome_options.add_argument('--window-size=1920x1080') chrome_options.add_argument('--disable-gpu') chrome_path = 'D://work/chromedriver.exe' driver = webdriver.Chrome(chrome_path,chrome_options=chrome_options) #start to crawl driver.get('https://www1.oanda.com/currency/converter/') time.sleep(1) end_date_input = driver.find_element_by_id('end_date_input') end_date_input.send_keys(Keys.CONTROL + 'a') time.sleep(1) end_date_input.send_keys(Keys.DELETE) time.sleep(1) end_date_input.send_keys('2019-09-06') time.sleep(1) end_date_input.send_keys(Keys.ENTER) time.sleep(1) soup = BeautifulSoup(driver.page_source, 'html.parser') table_soup= soup.findAll('tr',{'class':'body'})[1] bidAskAskAvg = round(float(table_soup.findAll('td')[4].text),8) print(bidAskAskAvg) -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 101.12.49.69 (臺灣) ※ 文章網址: https://www.ptt.cc/bbs/Python/M.1567942701.A.263.html

09/08 19:54, 5年前 , 1F
Chrome version: 75.0.3770.80
09/08 19:54, 1F

09/08 21:48, 5年前 , 2F
我看了一下網頁,你直接request 比較快,他還很友善的直
09/08 21:48, 2F

09/08 21:48, 5年前 , 3F
接回給你json,連parse html都不用。
09/08 21:48, 3F

09/09 00:06, 5年前 , 4F
有解了..改成09/06/2019就可, 謝謝樓上回覆,我也來試試
09/09 00:06, 4F
文章代碼(AID): #1TTEWj9Z (Python)
文章代碼(AID): #1TTEWj9Z (Python)