Python Selenium Geckodriver Detected as Bot



我试图刮网站skyscanner.com,他们似乎很容易检测到我的代码作为一个机器人。我尝试了新的旋转代理,99%的情况下,他们会在我第一次搜索时显示验证码。我将此代码用于其他网站,但没有发生这种情况,因此我不知道还需要更改什么以避免如此迅速地被检测到。我看到的另一个危险信号是,如果我在我的个人电脑上尝试相同的代理,我看不到验证码。所以它必须在selenium上进行一些设置。

下面是我创建一个新驱动程序的方法:
def getDriver(proxy,proxy_port)
proxy_profile = webdriver.FirefoxProfile()
# Proxy setup
proxy_profile.set_preference("network.proxy.type", 1)
proxy_profile.set_preference("network.proxy.http",proxy)
proxy_profile.set_preference("network.proxy.http_port",int(proxy_port))
proxy_profile.set_preference("network.proxy.https",proxy)
proxy_profile.set_preference("network.proxy.https_port",int(proxy_port))
proxy_profile.set_preference("network.proxy.ssl",proxy)
proxy_profile.set_preference("network.proxy.ssl_port",int(proxy_port))
proxy_profile.set_preference("network.proxy.ftp",proxy)
proxy_profile.set_preference("network.proxy.ftp_port",int(proxy_port))
proxy_profile.set_preference("network.proxy.socks",proxy)
proxy_profile.set_preference("network.proxy.socks_port",int(proxy_port))
# Remote DNS test
proxy_profile.set_preference("network.proxy.socks_remote_dns", True)
# Disable CSS (breaks the done loading condition)
#proxy_profile.set_preference('permissions.default.stylesheet', 2)
# Disable images
proxy_profile.set_preference('permissions.default.image', 2)
# Disable flash
proxy_profile.set_preference('dom.ipc.plugins.enabled.libflashplayer.so','false')
# User agent
self.info(self.userAgent)
proxy_profile.set_preference("general.useragent.override", self.userAgent)
# Avoid detection variables
proxy_profile.set_preference("dom.webdriver.enabled", False)
proxy_profile.set_preference('useAutomationExtension', False)
options = Options()
options.add_argument("--headless")
return webdriver.Firefox(firefox_profile=proxy_profile, firefox_options=options)

有什么想法可以让它更难以被发现吗?谢谢!

我遇到过和你描述的一样的情况。我是这样解决的:

  1. 通过selenium发送请求
  2. 自行解决验证码
  3. 保存cookies
  4. 在以后的搜索中使用这些cookie来避免captcha

下面是保护和加载cookie的函数:

def safe_cookies(driver):
pickle_filename = "cookies.pkl"
pickle.dump(driver.get_cookies(), open(pickle_filename, "wb"))
def load_cookies(driver):
cookies = pickle.load(open("cookies.pkl", "rb"))
if len(cookies) > 0:
for cookie in cookies:
driver.add_cookie(cookie)
driver.refresh()

在你第一次加载页面时重新加载cookies:

...
driver.get("https://www.skyscanner.com/")
load_cookies(driver)
...

最新更新