Selenium WebDriver检测限制可提取数据(Python)

时间:2018-07-28 15:33:42

标签: python selenium selenium-webdriver web-scraping selenium-chromedriver

编码新手,目前在我的第一个项目中工作,遇到障碍。我正在尝试使用ChromeDriver + Selenium将航班数据提交到ITA Matrix航班搜索中,请等待,然后对所得的“每月价格图表”进行屏幕截图。

我的代码可能还没有(但是),但是可以工作...除了ChromeDriver + Selenium发现的航班价格与在“常规”铬上使用相同参数的人工搜索发现的航班价格完全不同。罪魁祸首?由于某些原因,在Selenium上,ITA Matrix自动将结果限制为单个航空公司,如此处所示:Different results when using Selenium + Python这是我的代码:

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import time

from selenium.webdriver.chrome.options import Options
chrome_options = Options()
#chrome_options.add_argument("--headless")
chrome_options.add_argument('--disable-infobars')
chrome_options.add_argument('--disable-extensions')
chrome_options.add_argument('--profile-directory=Default')
chrome_options.add_argument('--incognito')
chrome_options.add_argument('--disable-plugins-discovery')
chrome_options.add_argument('--start-maximized')
driver = webdriver.Chrome(chrome_options=chrome_options, executable_path="chromedriver.exe")
driver.delete_all_cookies()
print("Chrome Browser Launched")

url = 'http://matrix.itasoftware.com'
origin = 'Toronto Lester B. Pearson International, ON, Canada'
destination = 'San Francisco International, CA'
depart_date = '09/30/18'
stay_length = "8-14"
airlines = "AIRLINES AC AA UA"

driver.get(url)
driver.get_screenshot_as_file("before_.png")

#Locate and Input Origin Airport
Origin_Input = driver.find_element_by_id("cityPair-orig-0")
Origin_Input.send_keys(origin)
time.sleep(3)
Origin_Input.send_keys(Keys.RETURN)

#Locate and Input Destination Airport
Destination_Input = driver.find_element_by_id("cityPair-dest-0")
Destination_Input.send_keys(destination)
time.sleep(3)
Destination_Input.send_keys(Keys.RETURN)

#Check Box for Lowest Fares Calendar
driver.find_element_by_id("gwt-uid-168").click()

#Locate and Input Departure Date
Depart_Input = driver.find_element_by_id("calDate-0")
Depart_Input.send_keys(depart_date)
time.sleep(3)
Depart_Input.send_keys(Keys.TAB)

#Locate and Input Stay Length
Depart_Input = driver.find_element_by_id("calStay-0")
Depart_Input.send_keys(stay_length)
time.sleep(3)

#Submit
Depart_Input.send_keys(Keys.RETURN)

#waits for page to load
time.sleep(45) 

driver.get_screenshot_as_file("after.png")

print('done!')

#driver.quit()

我什至尝试使用高级控件选项来将特定的航空公司AITA代码强制输入搜索中。.

我最好的猜测:网站正在检测自动化,并将结果限制在一家航空公司(始发国的主要承运人)。

我尝试过的其他方法:  -无痕模式  -禁用插件  -禁用扩展  -开始最大化  -Firefox w。壁虎  - 删除cookies  -根据Can a website detect when you are using selenium with chromedriver?

修改chomedriver.exe以删除文档变量

很明显,我仍在学习,所以我正在努力找出下一步。.非常感谢任何帮助!

0 个答案:

没有答案