pyppeteer实现页面点击事件

2022-01-24  本文已影响0人  坏人_李哈哈
import asyncio
import time

from pyppeteer import launch
from pyppeteer_stealth import stealth


async def chinamobile(successNum):
    # launch方法会新建一个browser对象,然后赋值给browser
    browser = await launch({'headless': True})

    # 调用 newPage 方法相当于浏览器中新建了一个选项卡,同时新建了一个Page对象
    page = await browser.newPage()
    await page.setViewport({'width': 1366, 'height': 768})
    await page.setJavaScriptEnabled(enabled=True)
    # 防止页面识别出脚本(反爬虫关键语句)
    await stealth(page)

    cookies = {'name': 'PHPSESSID', 'value': 'n16ciiatbso45p22esl9gu1fb1', 'domain': 'shop.10086.cn', 'path': '/'}
    await page.setCookie(cookies)
    cookies = {'name': 'c', 'value': 'mhrz1b6ee5784957a796cf739ef16d34', 'domain': '.10086.cn', 'path': '/'}
    await page.setCookie(cookies)
    cookies = {'name': 'cmccssotoken', 'value': 'mhrz1b6ee5784957a796cf739ef16d34@.10086.cn', 'domain': '.10086.cn'}
    await page.setCookie(cookies)
    cookies = {'name': 'defaultloginuser_p',
               'value': 'izr73fwOUuimT7R+YElqbvQdIEKrmWCpu49KY4pe7cglQnOlbxDN0nqcpR0yt5wiosjz2S6ZqH5XVmO8JcSRCCMTsRVyT13VInOal6sQlEY+dvBVErR/ksPv5W6XILGzNIChi3gihwmhVzzoGOae/UBKB06SDKI89CJbirq0ZmuNwSyqpJkEqg/LuT1QHsyO',
               'domain': '.10086.cn'}
    await page.setCookie(cookies)
    cookies = {'name': 'is_login', 'value': 'true', 'domain': '.10086.cn'}
    await page.setCookie(cookies)
    cookies = {'name': 'jsessionid-cmcc', 'value': 'nED55940BAA163B85D9F0B852C80321DB-1', 'domain': '.10086.cn'}
    await page.setCookie(cookies)
    cookies = {'name': 'key4IE89',
               'value': '32699DA99145B4A66AE4398C2B351A1952D212ADFD6EDB5316FFAF1FC5832DE9D0803E46EB86C64D83F5C9D60E831B09',
               'domain': '.10086.cn'}
    await page.setCookie(cookies)
    cookies = {'name': 'rpToken',
               'value': '5z7g3X4geXeR0Y1O0F1899emcN2V0%2F1o4H4A7u3I0H1Aa%3Df%3D00017a7630b23abf',
               'domain': '.10086.cn'}
    await page.setCookie(cookies)
    cookies = {'name': 'sendflag',
               'value': '20220123235110908300',
               'domain': '.10086.cn'}
    await page.setCookie(cookies)
    cookies = {'name': 'shopuid',
               'value': '54c3a8e0-57db-478d-935e-50f772eeb01c',
               'domain': 'shop.10086.cn'}
    await page.setCookie(cookies)
    cookies = {'name': 'cookieActived',
               'value': 'true',
               'domain': 'shop.10086.cn'}
    await page.setCookie(cookies)
    cookies = await page.cookies()
    # 调用了Page对象的goto方法就相当于在浏览器中输入问卷的网址,浏览器跳转到了对应的页面进行加载
    await page.goto('https://shop.10086.cn/goods/991_991_1085836_1074912.html')

    await asyncio.sleep(1)
    button = await page.querySelector(
        'body > div.content.clearfix > div.goodsInfo > div.phone_module > dl.clearfix.btnChose.checkCursor > dd > a')
    await button.click()
    await asyncio.sleep(1)
    queding = await page.querySelector('#popHeyue > div.popMain > div > p.globalMargin.aligncenter > a')
    await queding.click()

    button1 = await page.querySelector(
        '#buy_area > a.btn.btnBig.btnPink.ac_cart_buy.floatleft.marginRight10.btnBuy.click_float_buy')
    await button1.click()

    await asyncio.sleep(3)

    page_list = await browser.pages()
    page2 = page_list[-1]
    buttonfinal = await page2.querySelector(
        'body > div.content > div > div > p.aligncenter.margin20 > a')
    await asyncio.sleep(2)
    await buttonfinal.click()
    successNum = successNum + 1
    localtime = time.asctime(time.localtime(time.time()))

    print('成功了',successNum,'次!',localtime)
    await asyncio.sleep(3)
    await browser.close()
    return successNum


async def main():
    successNum = 0
    errorNum = 0
    while True:
        try:
            successNum = await chinamobile(successNum)
        except:
            localtime = time.asctime(time.localtime(time.time()))
            errorNum = errorNum + 1
            print('失败了',errorNum,'次!',localtime)
            continue

asyncio.get_event_loop().run_until_complete(main())

上一篇下一篇

猜你喜欢

热点阅读