from pyppeteer import launcher
launcher.DEFAULT_ARGS.remove("--enable-automation")
from pyppeteer_stealth import stealth
from pyppeteer import launch
# pyppeteer主函数
async def main():
bro = await launch({'headless': False,
'userDataDir': 'pyppeteer_data', # 指定浏览器缓存存放文件夹
'args': ['--no-sandbox',
'--start-maximized',
'--disable-gpu',
'--disable-blink-features=AutomationControlled',
'--disable-infobars',
'--user-agent=Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.107 Safari/537.36',
# '--proxy-server={}'.format(proxy)
],
'dumpio': True
})
page = await browser.newPage()
await stealth(page)
# 执行一系列js代码进行伪装
# 绕开webdriver检测
await page.evaluateOnNewDocument(
'''() =>{ Object.defineProperties(navigator,{ webdriver:{ get: () => false } }) }''')
# Pass the Chrome Test
await page.evaluateOnNewDocument('''() =>{ window.navigator.chrome = { runtime: {}, }; }''')
# Pass the Languages Test
await page.evaluateOnNewDocument(
'''() =>{ Object.defineProperty(navigator, 'languages', { get: () => ['en-US', 'en'] }); }''')
# Pass the Plugins Length Test
await page.evaluateOnNewDocument(
'''() =>{ Object.defineProperty(navigator, 'plugins', { get: () => [1, 2, 3, 4, 5,6], }); }''')
# Pass the Webdriver Test
await page.evaluateOnNewDocument('''() => {
const newProto = navigator.__proto__;
delete newProto.webdriver;
navigator.__proto__ = newProto;
}''')
# Pass the Permissions Test
await page.evaluateOnNewDocument('''() => {
const originalQuery = window.navigator.permissions.query;
window.navigator.permissions.__proto__.query = parameters =>
parameters.name === 'notifications'
? Promise.resolve({state: Notification.permission})
: originalQuery(parameters);
const oldCall = Function.prototype.call;
function call() {
return oldCall.apply(this, arguments);
}
Function.prototype.call = call;
const nativeToStringFunctionString = Error.toString().replace(/Error/g, "toString");
const oldToString = Function.prototype.toString;
function functionToString() {
if (this === window.navigator.permissions.query) {
return "function query() { [native code] }";
}
if (this === functionToString) {
return nativeToStringFunctionString;
}
return oldCall.call(oldToString, this);
}
Function.prototype.toString = functionToString;
}''')
# Pass the iframe Test
await page.evaluateOnNewDocument('''() => {
Object.defineProperty(HTMLIFrameElement.prototype, 'contentWindow', {
get: function() {
return window;
}
});
}''')
# Pass toString test, though it breaks console.debug() from working
await page.evaluateOnNewDocument('''() => {
window.console.debug = () => {
return null;
};
}''')
await bro.close()
# 启动main函数
asyncio.get_event_loop().run_until_complete(main())