作者shancool (酷扇)
看板Python
标题[问题] selenium headless send_Keys
时间Sun Sep 8 19:38:17 2019
各位版友好,
小弟爬虫学了一段时间,但这个问题卡了很久..
我的目标是:我想利用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://webptt.com/cn.aspx?n=bbs/Python/M.1567942701.A.263.html
1F:→ shancool: Chrome version: 75.0.3770.80 09/08 19:54
2F:推 rexyeah: 我看了一下网页,你直接request 比较快,他还很友善的直 09/08 21:48
3F:→ rexyeah: 接回给你json,连parse html都不用。 09/08 21:48
4F:→ shancool: 有解了..改成09/06/2019就可, 谢谢楼上回覆,我也来试试 09/09 00:06