【发布时间】:2020-09-16 22:37:58
【问题描述】:
上下文
我目前正在尝试使用 Python 中的 Selenium 和 Requests 模块构建一个小型机器人。
但是,我要与之交互的网页在 Cloudflare 后面运行。
我的 python 脚本正在使用 stem 模块在 Tor 上运行。
我的流量分析是基于 Firefox 的“开发者选项->网络”使用 Persist Logs。
到目前为止我的发现:
- Selenium 的 Firefox webdriver 可以经常访问网页,而无需经过“检查浏览器页面”(返回码 503)和“验证码页面”(返回码 403)。
- 使用相同的用户代理请求会话对象总是导致“验证码页面”(返回码 403)。
如果 Cloudflare 正在检查我的 Javascript 功能,我的请求模块不应该返回 503 吗?
代码示例
driver = webdriver.Firefox(firefox_profile=fp, options=fOptions)
driver.get("https://www.cloudflare.com") # usually returns code 200 without verifying the browser
session = requests.Session()
# ... applied socks5 proxy for both http and https ... #
session.headers.update({"user-agent": driver.execute_script("return navigator.userAgent;")})
page = session.get("https://www.cloudflare.com")
print(page.status_code) # return code 403
print(page.text) # returns "captcha page"
Selenium 和 Requests 模块都使用相同的用户代理和 ip。
两者都使用不带任何参数的 GET。
Cloudflare 如何区分这些流量?
我错过了什么吗?
我尝试将 cookie 从网络驱动程序传输到请求会话,以查看是否可以绕过但没有运气。
这是使用的代码:
for c in driver.get_cookies():
session.cookies.set(c['name'], c['value'], domain=c['domain'])
【问题讨论】:
-
使用 Web 驱动程序时需要考虑很多事情,包括但不限于; JavaScript API、HTTP 标头、TLS 标头、TCP 指纹、IP 指纹等。当使用 Selenium 等 Web 驱动程序时,Cloudflare 会将您标记为比请求模块时“更安全”。您需要修改请求的许多部分才能获得可扩展的解决方案。
标签: python selenium python-requests web-crawler cloudflare