用 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 的。所以不清楚哪里还需要设置。
用正常 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 的。所以不清楚哪里还需要设置。
