如何让selenium规避检测
当selenium登录时滑动总报错时,此时很大可能时selenium被检测到了,此时处理方法如下:
当selenium自动滑动滑块登录时报"哎呀,出错了,点击刷新再来一次(error:Gh3M9)"
处理代码如下:
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver import ChromeOptions
from selenium import webdriver
class TrainSpider(object):
def __init__(self, start_station, destinty_station, train_date, ticket_infos, passengers):
"""
:param start_station: 出发地
:param destinty_station: 目的地
:param train_date: 出发日期
:param ticket_infos: 车次以及坐席 {"G6325": ['O', 'M']}
:param passengers: 乘客信息
"""
self.url = 'https://kyfw.12306.cn/otn/resources/login.html'
self.passengers_url = 'https://kyfw.12306.cn/otn/confirmPassenger/initDc'
self.pay_url = 'https://kyfw.12306.cn/otn//payOrder/init?random=1631263890402'
self.payway_url = 'https://epay.12306.cn/pay/payGateway'
self.start_station = start_station
self.destinty_station = destinty_station
self.train_date = train_date
self.query_url = 'https://kyfw.12306.cn/otn/leftTicket/init?linktypeid=dc'
self.personal_url = 'https://kyfw.12306.cn/otn/view/index.html'
self.stations_dict = {}
self.station_code()
self.start_station_code = self.stations_dict[self.start_station].strip()
self.destinty_station_code = self.stations_dict[self.destinty_station].strip()
self.ticket_infos = ticket_infos
self.passengers = passengers
self.seat_type = None
self.options = ChromeOptions()
self.options.add_argument("--disable-blink-features=AutomationControlled")
self.options.add_experimental_option('excludeSwitches', ['enable-automation'])
self.driver = webdriver.Chrome(options=self.options)
主要加入两行代码:
self.options = ChromeOptions()
self.options.add_argument("--disable-blink-features=AutomationControlled")
self.options.add_experimental_option('excludeSwitches', ['enable-automation'])
这样再去登录12306滑动滑块就可以正常滑动不会报错了!!
|