selenium chrome 爬网站出现 412 问题

217 天前
 guoguobaba
用 python 写了一个 selenium 的网站爬虫,发现爬某一网站不成功。用 f12 看,显示 400 bad request

用正常 chrome 看了一下交互过程,发现

1. 打开 http://xxx.com ,返回 412 `< HTTP/1.1 412 Precondition Failed`

2. 然后访问一个 js ,js 设置 cookie ,

3. 带上 cookie 重新访问 http://xxx.com ,这时候返回 200

如果访问这个 xxx.com 没有带正确的 cookie ,就会显示 400 bad request 。 我在打开 chrome 的时候,清掉所有 cookie ,这时候能到第二步,但是第三步还是显示 cookie 错误,400 bad request

用 firefox 则不存在问题。所以怀疑 js 里针对 chrome 做了什么限制。

在打开 driver 时,我参考网上的把所有该加的都加了,也无效。
```
service = ChromeService(driver_path)
options = webdriver.ChromeOptions()
options.add_argument("--disable-blink-features=AutomationControlled")
options.add_argument("--disable-blink-features")
options.add_argument("--disable-extensions")
options.add_argument("--incognito")
options.add_experimental_option("excludeSwitches", ["enable-automation"])
options.add_experimental_option("useAutomationExtension", False)
```

console 里看 window.navigation.webdriver 也是 false 的。所以不清楚哪里还需要设置。
1247 次点击
所在节点    Python
4 条回复
cherryas
217 天前
那你就用 firefox 呗。为啥偏要用 chrome ?
Ruhaii
217 天前
这个就是大名鼎鼎的瑞数反爬虫,瑞数中会有检测是不是使用自动化工具的条件。如果真要用建议你去看看 怎么抹除自动化的特征
jeeyong
211 天前
很久没搞 selenium 类的东西了, 不知道还跟得上节奏不.
1. 检查 navigator.userAgent
2. 应该是 navigator.webdriver 吧?
3. $cdc_asdjflasutopfhvcZLmcfl_ 删了吗? 他应该是挂载再 window 下面的. 我之前都是编译 chromedriver 前删掉.
4. 不要直接修改 webdriver 的值. 用这个试试

br.execute_cdp_cmd('Page.addScriptToEvaluateOnNewDocument', {'source': """
Object.defineProperty(Navigator.prototype, 'webdriver', {
set: undefined,
enumerable: true,
configurable: true,
get: new Proxy(
Object.getOwnPropertyDescriptor(Navigator.prototype, 'webdriver').get,
{ apply: (target, thisArg, args) => {
// emulate getter call validation
Reflect.apply(target, thisArg, args);
return undefined;
}}
)
});
"""})
jeeyong
211 天前
或者你可以写一个 js, 在加载页面前载入, 然后记录他所有访问过的对象和方法..
再逐个去分析应该伪造哪些玩意...
我干出飞蚊症之后, 老老实实写 curd 的东西了...太累了.

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/982750

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX