避免检测设置:参考:https://blog.csdn.net/weixin_41173374/article/details/100625270
设置教程:
此时,你可以在PyCharm中,按住Command键(Windows、Linux用户按住Ctrl键),鼠标左键点击 frompyppeteerimportlaunch中的 launch,自动跳转到Pyppeteer源代码中的 launcher.py文件
把代码往上翻,在第61行左右,找到如下的代码:
AUTOMATION_ARGS = [
'--enable-automation',
'--password-store=basic',
'--use-mock-keychain',
将其改成:
AUTOMATION_ARGS = [
# '--enable-automation', # 注释掉可以设置window.nagivator.webdriver为undefined
'--password-store=basic',
'--use-mock-keychain',
]
说明: userDataDir 临时文件夹最好指定,不然偶尔报错 和 executablePath 参数指定谷歌浏览器启动路径
browser = await launch({'headless': False,
'dumpio': True,
'args': ['--no-sandbox'],
# '--window-size': '2000,2000',
'userDataDir': r'D:\Google\tempprorag',
'executablePath': r'D:\Google\Chrome\Application\chrome.exe', }) # 启动pyppeteer 属于内存中实现交互的模拟器
代码仅供参考:
# -*- coding:utf-8 -*-
import asyncio
import time, random
import pyautogui as ckpg
from pyppeteer.launcher import launch # 控制模拟浏览器用
def moveRandom(num):
for i in range(0, num):
s = random.random()
x = random.randint(100, 1000)
y = random.randint(100, 1000)
ckpg.moveTo(x, y, duration=s)
print("移动轨迹:", x, y, s)
def MoveClickSend(x_zb, y_zb, chrstr):
moveRandom(4)
# 移动到指定位置
ckpg.moveTo(x_zb, y_zb, duration=0.25)
# 单击鼠标左键
ckpg.click() # x=x_zb, y=y_zb, button='left'
# 输入字符
print("输入:", chrstr)
ckpg.keyDown('shift')
ckpg.keyUp('shift')
chr_list = list(chrstr)
for i in chr_list:
ckpg.press(i)
ckpg.keyDown('shift')
ckpg.keyUp('shift')
# 携程调用chorme.exe
async def main(username, password, url):
# 以下使用可以针对耗时的操作进行挂起
browser = await launch({'headless': False,
'dumpio': True,
'args': ['--no-sandbox'],
# '--window-size': '2000,2000',
'userDataDir': r'D:\Google\tempprorag',
'executablePath': r'D:\Google\Chrome\Application\chrome.exe', }) # 启动pyppeteer 属于内存中实现交互的模拟器
page = await browser.newPage() # 启动个新的浏览器页面
await page.setViewport({
"width": 1000,
"height": 1000
})
await page.setUserAgent(
'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 Safari/537.36')
res = await page.goto(url) # 访问登录页面
# 百度输入框,输入哈哈
await page.type('#kw', username, {'delay': 50})
# await page.type('#TPL_username_1', username, {'delay': 50})
# await page.type('#TPL_password_1', password, {'delay': 100})
# x, y = pag.position() # 返回当前鼠标的坐标
# 输入用户名和密码,这里利用鼠标和键盘的控制模块pyautogui, 前两个参数是x, y 坐标
# MoveClickSend(274, 433, username)
# MoveClickSend(271, 501, password)
# 点击登录
# ckpg.moveTo(400, 641, duration=0.25)
# ckpg.mouseDown()
# ckpg.mouseUp()
# moveRandom(4)
# data = await page.content()
resp_cookies = await page.cookies() # cookie
resp_status = res.status # 响应状态
print("resp_cookies", resp_cookies)
print("resp_status", resp_status)
print("关闭浏览器")
await browser.close()
return resp_cookies
# 主程序入口
if __name__ == '__main__':
username = "haha"
password = "xxxx"
url = "https://www.baidu.com"
# 获取登录后的用户cookie
resp_cookies = asyncio.get_event_loop().run_until_complete(main(username, password, url))
resp_cookies = ""
for ck in resp_cookies:
name = ck.get('name')
value= ck.get('value')
print(name ,"=", value)