我们再使用pyppeteer启动浏览器的时候,如果没有添加一些操作,浏览器就会显示Chrome正受到自动测试软件,如以下代码:
import asyncio from pyppeteer import launch class baidu_Click(): def __init__(self): super(baidu_Click, self).__init__() async def set_browser(self): self.browser=await launch(headless=False) pages=await self.browser.pages() self.page=pages[0] await self.page.goto('https://www.baidu.com') await asyncio.sleep(10) await self.browser.close() def qidong(self): asyncio.get_event_loop().run_until_complete(self.set_browser()) if __name__ == '__main__': baidu=baidu_Click() baidu.qidong()
运行效果如下
这样会被反爬虫机制检测到,所以我们要去除,方法有两种,具体如下:
import asyncio from pyppeteer import launch,launcher class baidu_Click(): def __init__(self): super(baidu_Click, self).__init__() async def set_browser(self): self.browser=await launch(headless=False,ignoreDefaultArgs=['--enable-automation']) pages=await self.browser.pages() self.page=pages[0] await self.page.goto('https://www.baidu.com') await asyncio.sleep(10) await self.browser.close() def qidong(self): asyncio.get_event_loop().run_until_complete(self.set_browser()) if __name__ == '__main__': baidu=baidu_Click() baidu.qidong()
或者:
import asyncio from pyppeteer import launch,launcher launcher.DEFAULT_ARGS.remove("--enable-automation") class baidu_Click(): def __init__(self): super(baidu_Click, self).__init__() async def set_browser(self): self.browser=await launch(headless=False) pages=await self.browser.pages() self.page=pages[0] await self.page.goto('https://www.baidu.com') await asyncio.sleep(10) await self.browser.close() def qidong(self): asyncio.get_event_loop().run_until_complete(self.set_browser()) if __name__ == '__main__': baidu=baidu_Click() baidu.qidong()
最后运行结果都不会再显示Chrome正受到自动测试软件的控制
评论