|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | +# @Author: Kris |
| 3 | +# @Mail: criselyj@163.com |
| 4 | +# @Date: 2020年08月14日 17:40:11 |
| 5 | +import os |
| 6 | +import random |
| 7 | +import asyncio |
| 8 | +from pyppeteer import launch |
| 9 | + |
| 10 | +base_url = 'https://passport.xiami.com/' |
| 11 | +current_dir = os.path.dirname(os.path.realpath(__file__)) |
| 12 | +# Fix:https://github.com/miyakogi/pyppeteer/issues/183 文件权限问题。 |
| 13 | +cache_dir = os.path.join(current_dir, 'cache') |
| 14 | +if not os.path.exists(cache_dir): |
| 15 | + os.mkdir(cache_dir) |
| 16 | + |
| 17 | + |
| 18 | +class XMLogin(object): |
| 19 | + url = base_url |
| 20 | + |
| 21 | + def __init__(self, account, password): |
| 22 | + self.account = account |
| 23 | + self.password = password |
| 24 | + self.browser = None |
| 25 | + self.page = None |
| 26 | + |
| 27 | + async def send_key(self): |
| 28 | + await self.page.click('.login-switch') |
| 29 | + await self.page.type('#account', self.account, |
| 30 | + {'delay': random.randint(100, 200) - 50}) |
| 31 | + await self.page.type('#password', self.password, |
| 32 | + {'delay': random.randint(100, 200) - 50}) |
| 33 | + |
| 34 | + async def slide(self): |
| 35 | + try: |
| 36 | + await self.page.hover('#captcha') |
| 37 | + await self.page.mouse.down() |
| 38 | + await self.page.mouse.move(2000, 0, |
| 39 | + {'delay': random.randint(2000, 4000)}) |
| 40 | + await self.page.mouse.up() |
| 41 | + except Exception as e: |
| 42 | + print('error', e) |
| 43 | + exit(0) |
| 44 | + |
| 45 | + async def validate(self): |
| 46 | + try: |
| 47 | + error_element = await self.page.xpath('//div[@id="error"]') |
| 48 | + msg = await ( |
| 49 | + await error_element[0].getProperty('textContent')).jsonValue() |
| 50 | + except Exception: |
| 51 | + return None |
| 52 | + return msg |
| 53 | + |
| 54 | + async def crawl(self): |
| 55 | + # 测试环境下 headless 设置为 False |
| 56 | + # 生产环境可以修改为无头浏览器 |
| 57 | + self.browser = await launch({ |
| 58 | + 'headless': False, |
| 59 | + 'userDataDir': cache_dir, |
| 60 | + 'args': ['--no-sandbox'] |
| 61 | + }) |
| 62 | + self.page = await self.browser.newPage() |
| 63 | + await self.page.goto(self.url) |
| 64 | + # 伪造当前浏览状态 防止自动化工具检测 |
| 65 | + codes = ( |
| 66 | + "() =>{ Object.defineProperties(navigator,{ webdriver:" |
| 67 | + "{ get: () => false } }) }", |
| 68 | + "() =>{ window.navigator.chrome = { runtime: {}, }; }", |
| 69 | + "() =>{ Object.defineProperty(navigator, 'languages', " |
| 70 | + "{ get: () => ['en-US', 'en'] }); }", |
| 71 | + "() =>{ Object.defineProperty(navigator, 'plugins', { " |
| 72 | + "get: () => [1, 2, 3, 4, 5,6], }); }" |
| 73 | + ) |
| 74 | + for code in codes: |
| 75 | + await self.page.evaluate(code) |
| 76 | + await self.send_key() |
| 77 | + await asyncio.sleep(random.randint(2, 3)) |
| 78 | + await self.slide() |
| 79 | + |
| 80 | + # 登录 |
| 81 | + await asyncio.sleep(random.randint(2, 3)) |
| 82 | + await self.page.click('#submit') |
| 83 | + msg = await self.validate() |
| 84 | + if msg: |
| 85 | + print('[*] 错误信息:', msg) |
| 86 | + exit(0) |
| 87 | + print('[*] 登录成功') |
| 88 | + await asyncio.sleep(5) |
| 89 | + |
| 90 | + |
| 91 | +def main(): |
| 92 | + print('[*] 模拟登陆虾米音乐程序启动...') |
| 93 | + account = input('[*] 请输入账号:') |
| 94 | + password = input('[*] 请输入密码:') |
| 95 | + login = XMLogin(account, password) |
| 96 | + loop = asyncio.get_event_loop() |
| 97 | + loop.run_until_complete(login.crawl()) |
| 98 | + |
| 99 | + |
| 100 | +if __name__ == '__main__': |
| 101 | + main() |
0 commit comments