V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
guoguobaba
V2EX  ›  Python

selenium chrome 爬网站出现 412 问题

  •  
  •   guoguobaba · 196 天前 · 1191 次点击
    这是一个创建于 196 天前的主题,其中的信息可能已经有所发展或是发生改变。
    用 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 的。所以不清楚哪里还需要设置。
    4 条回复    2023-10-23 10:49:45 +08:00
    cherryas
        1
    cherryas  
       196 天前
    那你就用 firefox 呗。为啥偏要用 chrome ?
    Ruhaii
        2
    Ruhaii  
       196 天前   ❤️ 1
    这个就是大名鼎鼎的瑞数反爬虫,瑞数中会有检测是不是使用自动化工具的条件。如果真要用建议你去看看 怎么抹除自动化的特征
    jeeyong
        3
    jeeyong  
       190 天前
    很久没搞 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
        4
    jeeyong  
       190 天前
    或者你可以写一个 js, 在加载页面前载入, 然后记录他所有访问过的对象和方法..
    再逐个去分析应该伪造哪些玩意...
    我干出飞蚊症之后, 老老实实写 curd 的东西了...太累了.
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   1145 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 27ms · UTC 17:51 · PVG 01:51 · LAX 10:51 · JFK 13:51
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.