赞
踩
# 执行js 点击
# js 代码
xx = """document.querySelector('a[data-logintype="phonepwd"]').click()"""
self.page.evaluate(xx)
# 直接点击
await self.page.click('div.account-center-submit')
# 获取本页url
middle_redirect_url = self.page.url
await self.page.reload()
await self.page.waitFor(1000) # 等待
await self.page.goto(src) # 重新请求页面
await self.page.content() # 获取页面内容
# 清空输入框
await self.page.evaluate('''document.querySelector('#loginname').value="" ''') # 清空输入框
// 对于id标签 使用 #
await self.page.click('#fb_login_phonepwd_btnyes')
// 对于 class标签 使用 .
await self.page.hover('div.secsdk-captcha-drag-icon')
// 对于 其它标签
await self.page.click('div[node-type="message_form"] > div > a > span[node-type="submitStates"]')
await self.page.type('#fb_login_phonepwd_pwd', username_, {'delay': random.randint(100, 151) - 50})
# 或者
input_user = await self.page.xpath("//input[@name='loginCode']")
await input_user[0].type(username_, {'delay': random.randint(100, 151) - 50})
image_src = await self.page.Jeval('div.JDJRV-img-wrap > div.JDJRV-bigimg >img', 'el => el.src')
# 或者
elements = await self.page.querySelectorAll("div.channels > div.id-list > div.id-item-container")
for element in elements:
''' 内容分为 客户 达人/创作者 '''
div_content = await(await element.querySelector('div.title')).getProperty('textContent')
# print(await div_content.jsonValue())
if '客户' in str(await div_content.jsonValue()):
await element.click()
break
# 或者
element = await page.querySelector('h1')
title = await self.page.evaluate('(element) => element.textContent', element)
phone = await pageX.querySelectorEval('div.ui-form-text', 'node => node.textContent')
await self.page.keyboard.press("Tab")
await self.page.keyboard.press("Enter")
# <frame name="leftiframe" id="leftiframe" scrolling="auto" marginwidth="0" marginheight="0" src="lefttree.aspx)">
SaveStudyRecord2 = '''document.querySelector('#rightiframe').contentWindow.document.querySelector('#SaveStudyRecord2')'''
await self.page.click(SaveStudyRecord2)
# 还可通过 获取
self.page.frames
async def get_cookie(self): cookies_list = await self.page.cookies() cookies = '' for cookie in cookies_list: str_cookie = '{0}={1}; ' str_cookie = str_cookie.format(cookie.get('name'), cookie.get('value')) cookies += str_cookie return cookies async def _injection_cookie(self, Cookies): """ 注入旧的cookie 方便登录 :param Cookies: :return: """ pass for _cookie in Cookies.split("; "): if len(_cookie.split("=")) >= 2: k, v = _cookie.split("=", 1) await self.page.setCookie({ 'name': k, 'value': v, 'domain': '.domain.com', 'path': '/', 'expires': 1605319285, 'size': 70, 'httpOnly': False, 'secure': False, 'session': False })
await self.page.evaluate('''$("button:contains('客户登录')").click()''')
async def mouse_slider_A(self): iframe_slide_move_js = ''' aaa = document.querySelector('#nc_2_n1z'); event = document.createEvent('MouseEvents'); event.initEvent('mousedown', true, false); aaa.dispatchEvent(event); event = document.createEvent('MouseEvents'); event.initEvent('mousemove', true, false); Object.defineProperty(event,'clientX',{get(){return %s;}}) aaa.dispatchEvent(event); ''' iframe_slide_up_js = ''' event.initEvent('mouseup', true, false); aaa.dispatchEvent(event); ''' _step = 0 step_total = 260 times = 3 # 滑动次数 for _s in range(times): if times == _s+1: _step = step_total else: _step += random.randint(70, 105) await self.page.evaluate(iframe_slide_move_js % _step) await asyncio.sleep(0.38) await asyncio.sleep(0.8) await self.page.evaluate(iframe_slide_up_js) async def mouse_slider_B(self): """滑动滑块 """ await asyncio.sleep(3) length_ = round(random.uniform(18, 22))*100 # 经测试 影响不大 _step = round(random.uniform(5, 15)) # 经测试 20不行 try: await self.page.hover('#nc_1_n1z') print("^*" * 20) # 鼠标按下按钮 await self.page.mouse.down() # 移动鼠标 # await asyncio.sleep(80) await self.page.mouse.move(length_, 0, {'steps': _step}) # steps 验证 0-15都可以 20不行 # 松开鼠标 await self.page.mouse.up() await asyncio.sleep(2) except Exception as e: print(e, ' :错误') return None else: await asyncio.sleep(3) # 获取元素内容 try: slider_again = await self.page.querySelectorEval('#nc_1__scale_text', 'node => node.textContent') if slider_again != '验证通过': return None else: print('验证通过') return True except: print(" 检验滑块是否通过 报错 ") return None async def mouse_slider_C(self): """ 分段滑动 """ _sider = '#nc_1_n1t' if not await self.page.J(_sider): print(" 没有滑块 ") return basic_length_ = lambda _x: _x+random.randint(0, 30) for length_ in [basic_length_(340) for _ in range(3)]: el = await self.page.J(_sider) box = await el.boundingBox() await self.page.hover(_sider) await self.page.mouse.down() await self.page.mouse.move(box['x'] + length_*random.randint(50, 70)*0.01, box['y'], {'steps': 30}) await self.page.waitFor(random.randint(300, 700)) await self.page.mouse.move(box['x'] + length_*random.randint(85, 92)*0.01, box['y'], {'steps': 30}) await self.page.waitFor(random.randint(300, 700)) # await self.page.mouse.move(box['x'] + distance + 29, box['y'], {'steps': 30}) await self.page.mouse.move(box['x'] + length_, box['y'], {'steps': 30}) await self.page.mouse.up() await asyncio.sleep(2) if not await self.page.J(_sider): break
ua = 'Mozilla/5.0 (iPhone; CPU iPhone OS 13_2_3 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/13.0.3 Mobile/15E148 Safari/604.1'
await self.page.setUserAgent(ua)
await self.page.setViewport({'width': 375, 'height': 812, "isMobile": True}) # isMobile 注意此参数
# fullPage代表全屏截图
await self.page.screenshot({'path': f'./img.jpeg', 'type': 'jpeg', 'fullPage': True})
import asyncio from pyppeteer import launch # 请求拦截器函数,设置拦截条件并可作修改 async def intercept_request(interceptedRequest): if 'xxx' in interceptedRequest.url: await interceptedRequest.continue_({"url": "xxx"}) # 修改url为xxx else: await interceptedRequest.continue_() # 响应拦截器函数,设置拦截条件并可作修改 async def intercept_response(interceptedResponse): print(interceptedResponse.url) if 'https://www.baidu.com/' == interceptedResponse.url: response = await interceptedResponse.text() # 获得请求的text内容 print(response) async def main(): launch_params = { 'headless': False, # 'userDataDir': './userdata', # 'userDataDir': r'C:\Users\{}\AppData\Local\Google\Chrome\User Data'.format(getpass.getuser()), 'args': [ '--disable-extensions', '--hide-scrollbars', '--disable-bundled-ppapi-flash', '--mute-audio', '--no-sandbox', '--disable-setuid-sandbox', '--disable-gpu', # '--disable-infobars' '--window-size=1866,768' # '--window-size={1300},{800}' ], 'dumpio': True } # browser = await launch(options={'devtools': True, 'headless': False}) browser = await launch(options=launch_params) page = await browser.newPage() # 设置浏览器大小 await page.setViewport({'width': 1300, 'height': 800}) await page.setRequestInterception(True) page.on('request', lambda req: asyncio.ensure_future(intercept_request(req))) # 设置response拦截器 page.on('response', lambda rep: asyncio.ensure_future(intercept_response(rep))) await page.goto('https://www.baidu.com') # await page.screenshot({'path': 'baidu.png'}) await asyncio.sleep(200) await browser.close() asyncio.get_event_loop().run_until_complete(main())
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。