selenium chrome 爬网站出现 412 问题 - V2EX
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 2023-10-17 14:14:04 +08:00 2078 次点击
    这是一个创建于 729 天前的主题,其中的信息可能已经有所发展或是发生改变。
    用 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  
       2023-10-17 14:23:30 +08:00
    那你就用 firefox 呗。为啥偏要用 chrome ?
    Ruhaii
        2
    Ruhaii  
       2023-10-17 21:36:36 +08:00   1
    这个就是大名鼎鼎的瑞数反爬虫,瑞数中会有检测是不是使用自动化工具的条件。如果真要用建议你去看看 怎么抹除自动化的特征
    jeeyong
        3
    jeeyong  
       2023-10-23 10:47:39 +08:00
    很久没搞 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  
       2023-10-23 10:49:45 +08:00
    或者你可以写一个 js, 在加载页面前载入, 然后记录他所有访问过的对象和方法..
    再逐个去分析应该伪造哪些玩意...
    我干出飞蚊症之后, 老老实实写 curd 的东西了...太累了.
    关于     帮助文档     自助推广系统     博客     API     FAQ     Solana     2713 人在线   最高记录 6679       Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 20ms UTC 15:00 PVG 23:00 LAX 08:00 JFK 11:00
    Do have faith in what you're doing.
    ubao msn snddm index pchome yahoo rakuten mypaper meadowduck bidyahoo youbao zxmzxm asda bnvcg cvbfg dfscv mmhjk xxddc yybgb zznbn ccubao uaitu acv GXCV ET GDG YH FG BCVB FJFH CBRE CBC GDG ET54 WRWR RWER WREW WRWER RWER SDG EW SF DSFSF fbbs ubao fhd dfg ewr dg df ewwr ewwr et ruyut utut dfg fgd gdfgt etg dfgt dfgd ert4 gd fgg wr 235 wer3 we vsdf sdf gdf ert xcv sdf rwer hfd dfg cvb rwf afb dfh jgh bmn lgh rty gfds cxv xcv xcs vdas fdf fgd cv sdf tert sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf shasha9178 shasha9178 shasha9178 shasha9178 shasha9178 liflif2 liflif2 liflif2 liflif2 liflif2 liblib3 liblib3 liblib3 liblib3 liblib3 zhazha444 zhazha444 zhazha444 zhazha444 zhazha444 dende5 dende denden denden2 denden21 fenfen9 fenf619 fen619 fenfe9 fe619 sdf sdf sdf sdf sdf zhazh90 zhazh0 zhaa50 zha90 zh590 zho zhoz zhozh zhozho zhozho2 lislis lls95 lili95 lils5 liss9 sdf0ty987 sdft876 sdft9876 sdf09876 sd0t9876 sdf0ty98 sdf0976 sdf0ty986 sdf0ty96 sdf0t76 sdf0876 df0ty98 sf0t876 sd0ty76 sdy76 sdf76 sdf0t76 sdf0ty9 sdf0ty98 sdf0ty987 sdf0ty98 sdf6676 sdf876 sd876 sd876 sdf6 sdf6 sdf9876 sdf0t sdf06 sdf0ty9776 sdf0ty9776 sdf0ty76 sdf8876 sdf0t sd6 sdf06 s688876 sd688 sdf86