Selenium反监测方法

看到论坛之前有人提问过,最近正好我也在研究
需要用到mitmproxy,和一点点js技巧

from mitmproxy import ctx
injected_javascript = ‘’’
// overwrite the languages property to use a custom getter
Object.defineProperty(navigator, “languages”, {
get: function() {
return [“zh-CN”,“zh”,“zh-TW”,“en-US”,“en”];
}
});

// Overwrite the plugins property to use a custom getter.
Object.defineProperty(navigator, ‘plugins’, {
get: () => [1, 2, 3, 4, 5],
});

// Pass the Webdriver test
Object.defineProperty(navigator, ‘webdriver’, {
get: () => false,
});

// Pass the Chrome Test.
// We can mock this in as much depth as we need for the test.
window.navigator.chrome = {
runtime: {},
// etc.
};

// Pass the Permissions Test.
const originalQuery = window.navigator.permissions.query;
window.navigator.permissions.query = (parameters) => (
parameters.name === ‘notifications’ ?
Promise.resolve({ state: Notification.permission }) :
originalQuery(parameters)
);
‘’’

def response(flow):
# Only process 200 responses of HTML content.
if not flow.response.status_code == 200:
return

# Inject a script tag containing the JavaScript.
html = flow.response.text
html = html.replace('<head>', '<head><script>%s</script>' % injected_javascript)
flow.response.text = str(html)
ctx.log.info('>>>> js代码插入成功 <<<<')

亲测可以过淘宝的检测,从js代码来看算是比较通用的解法,特定网站思路其实类似

这个可以哦。但是不知道能不能过谷歌 facebook这些网站的检测,这些站可能检测的东西更多

如果采用了mitmproxy的话怎么对接第三方proxy的api呢?

网上我看是有相关的解决方法的,具体我还没测试,但一个最简单的思路,就是把第三方的proxy设置为全局的proxy

还是用 Puppeteer 吧,相关 anti-detect 插件很多。

主要就是这个了,其他的都是些零零碎碎的脚本。功能跟这个差不多。而且这不是一个插件,这是一系列插件中的一个。