21番茄网

 找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
热搜: 21番茄网
查看: 800|回复: 0

selenium避免被识别的方法

[复制链接]

646

主题

799

帖子

2686

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
2686
发表于 2022-4-20 15:51:29 | 显示全部楼层 |阅读模式
selenium避免被识别的方法:

方法一:加上避免识别的代码
from selenium import webdriver # 从selenium库中调用webdriver模块

options = webdriver.ChromeOptions()
options.add_experimental_option('excludeSwitches', ['enable-automation'])
options.add_experimental_option('useAutomationExtension', False)
options.add_argument("--disable-blink-features")
options.add_argument("--disable-blink-features=AutomationControlled")
driver = webdriver.Chrome(options=options)   # 设置引擎为Chrome,真实地打开一个Chrome浏览器
driver.execute_cdp_cmd("Page.addScriptToEvaluateOnNewDocument", {
  "source": """
    Object.defineProperty(navigator, 'webdriver', {
      get: () => undefined
    })
  """
})

driver.get(网页链接)    # 括号中输入链接


方法二:selenium调用现有真正人工使用的google浏览器
打开电脑的cmd,输入以下代码:
"C:\Program Files\Google\Chrome\Application\chrome.exe" --remote-debugging-port=9222

然后在python中输入以下代码:
options=Options()
options.add_experimental_option("debuggerAddress","localhost:9222")
driver = webdriver.Chrome(executable_path=r"D:\ProgramData\Anaconda3\chromedriver.exe",options=options)

driver.get(网页链接)    # 括号中输入链接


方法三:在方法一和方法二的基础上,使用mitmproxy:
1.安装mitmproxy:
(1)在cmd中输入:pip install mitmproxy  来进行安装
(2)http://mitm.it/  或  https://docs.mitmproxy.org/stable/overview-installation/  或 https://mitmproxy.org/  中下载安装证书

注意:如果在pip安装mitmproxy时,提示ruamel报错,无法卸载之类的情况,删除anaconda/Lib/site-packages下所有的ruamel相关的的文件夹和文件。
详见:https://blog.csdn.net/cc9200/article/details/115016355

2.准备py文件,名称随意,这里写为:daili.py
from mitmproxy import ctx

def response(flow):
    """修改响应数据
    """
    if '/js/yoda.' in flow.request.url:     #注意:'/js/yoda.'   仅针对淘宝而言,其他网站请自行修改
        # 屏蔽selenium检测
        for webdriver_key in ['webdriver', '__driver_evaluate', '__webdriver_evaluate', '__selenium_evaluate',
                              '__fxdriver_evaluate', '__driver_unwrapped', '__webdriver_unwrapped',
                              '__selenium_unwrapped', '__fxdriver_unwrapped', '_Selenium_IDE_Recorder', '_selenium',
                              'calledSelenium', '_WEBDRIVER_ELEM_CACHE', 'ChromeDriverw', 'driver-evaluate',
                              'webdriver-evaluate', 'selenium-evaluate', 'webdriverCommand',
                              'webdriver-evaluate-response', '__webdriverFunc', '__webdriver_script_fn',
                              '__$webdriverAsyncExecutor', '__lastWatirAlert', '__lastWatirConfirm',
                              '__lastWatirPrompt', '$chrome_asyncScriptInfo', '$cdc_asdjflasutopfhvcZLmcfl_']:
            ctx.log.info('Remove "{}" from {}.'.format(webdriver_key, flow.request.url))
            flow.response.text = flow.response.text.replace('"{}"'.format(webdriver_key), '"NO-SUCH-ATTR"')
            print(webdriver_key)
        flow.response.text = flow.response.text.replace('t.webdriver', 'false')
        flow.response.text = flow.response.text.replace('ChromeDriver', '')

以上文件写完后保存。

3.在CMD中,输入以下代码,之后不要关闭窗口:
mitmdump.exe -p 8080  -s 此处输入daili.py的绝对地址\daili.py

4.在python中,写入方法一或方法二的代码,并运行即可。


来自:https://www.icode9.com/content-4-946623.html

selenium避免被识别的方法  Python爬虫精进 1
                               
登录/注册后可看大图



方法四:在方法一、二、三还不行的基础上,使用更换ip地址,更换账号,控制采集总时间、采集数量、采集间隔时间等方式避免被服务器发现。实在不行,就休息休息,过段时间再来。









上一篇:selenium添加参数,让网站无法识别是selenium
下一篇:常用的python爬虫工具汇总
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|Archiver|手机版|小黑屋|21番茄网 ( 粤ICP备10099428号 ) 奇远富

GMT+8, 2024-4-26 15:06 , Processed in 0.263885 second(s), 34 queries .

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表