1. 启动浏览器
import asyncio
import tkinter
from pyppeteer import launch
from config.config import start_parm
from pyppeteer_stealth import stealth
async def main(tel):
browser = await launch(**start_parm)
page = await browser.newPage()
await stealth(page)
tk = tkinter.Tk()
width = tk.winfo_screenwidth()
height = tk.winfo_screenheight()
tk.quit()
await page.setViewport(viewport={'width': width, 'height': height})
1.1 start_parm 浏览器环境配置项
start_parm = {
"headless": False,
"userDaraDir":r"D:\data",
"autoClose": True,
"args": [
'--disable-infobars',
'--start-maximized',
],
}
2. 请求网页
await page.goto('url')
3. 获取元素的文本内容
element = await page.querySelector('selector')
print(element)
content = await (await element.getProperty('textContent')).jsonValue()
print(content)
4. 截图
el = await page.J(
'.shumei_captcha_img_wrapper')
box = await el.boundingBox()
print('bg', box)
await page.screenshot({'path': 'yilong.png', 'clip': box})
await page.waitFor(2000)
5. move移动

async def get_images(page, _selector=None, pngName=None):
await page.waitFor(_selector)
el = await page.J(_selector)
box = await el.boundingBox()
await page.screenshot({'path': pngName, 'clip': box})
await page.waitFor(2000)
distance = await verify_code(pngName)
return distance
6. 填写限制速度
await page.type('queryselector', '文本', {'delay': 30})
7.xpath使用
verify = await page.xpath('//*[@aria-label="xxx"]')
await verify[0].click()
await verify[0].type('text')
8. 当前页面的url
_url = page.url
9. 清除文本框内容
await page.evaluate('document.querySelector("#verifyCode").value=""')