背景
我正在尝试使用Python中的Selenium和Requests模块构建一个小型机器人。
然而,我想要互动的网页在Cloudflare后面运行。
我的python脚本正在使用stem模块通过Tor运行。
我的流量分析基于Firefox的“开发者选项->网络”使用持久记录。
到目前为止我的发现:
- Selenium的Firefox webdriver通常可以访问网页而无需经过“检查浏览器页面”(返回代码503)和“验证码页面”(返回代码403)。
- 使用相同用户代理的请求会话对象总是导致“验证码页面”(返回代码403)。
如果Cloudflare正在检查我的Javascript功能,那么我的requests模块不应该返回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 如何区分这些流量呢?
我是否漏掉了什么?
我尝试将 webdriver 中的 cookies 转移到 requests 会话中,以查看是否可能绕过 Cloudflare,但没有成功。
以下是使用的代码:
for c in driver.get_cookies():
session.cookies.set(c['name'], c['value'], domain=c['domain'])