zoukankan      html  css  js  c++  java
  • 【Python】selenium模拟淘宝登录

        # -*- coding: utf-8 -*-
         
        from selenium import webdriver
        from selenium.webdriver.common.by import By
        from selenium.webdriver.support.ui import WebDriverWait
        from selenium.webdriver.support import expected_conditions as EC
        import time
        
        #定义一个taobao类
        class taobao_infos:
        
            #对象初始化
            def __init__(self):
                url = 'https://login.taobao.com/member/login.jhtml'
                self.url = url
        
                options = webdriver.ChromeOptions()
        #        options.add_experimental_option("prefs", {"profile.managed_default_content_settings.images": 2}) # 不加载图片,加快访问速度
                options.add_experimental_option('excludeSwitches', ['enable-automation']) # 此步骤很重要,设置为开发者模式,防止被各大网站识别出来使用了Selenium
        
                self.browser = webdriver.Chrome(executable_path=chromedriver_path, options=options)
                self.wait = WebDriverWait(self.browser, 10) #超时时长为10s
        
        
            #登录淘宝
            def login(self):
        
                # 打开网页
                self.browser.get(self.url)
        
                # 等待 密码登录选项 出现
                password_login = self.wait.until(EC.presence_of_element_located((By.CSS_SELECTOR, '.qrcode-login > .login-links > .forget-pwd')))
                password_login.click()
        
                # 等待 微博登录选项 出现
                weibo_login = self.wait.until(EC.presence_of_element_located((By.CSS_SELECTOR, '.weibo-login')))
                weibo_login.click()
        
                # 等待 微博账号 出现
                weibo_user = self.wait.until(EC.presence_of_element_located((By.CSS_SELECTOR, '.username > .W_input')))
                weibo_user.send_keys(weibo_username)
        
                # 等待 微博密码 出现
                weibo_pwd = self.wait.until(EC.presence_of_element_located((By.CSS_SELECTOR, '.password > .W_input')))
                weibo_pwd.send_keys(weibo_password)
        
                # 等待 登录按钮 出现
                submit = self.wait.until(EC.presence_of_element_located((By.CSS_SELECTOR, '.btn_tip > a > span')))
                time.sleep(10)
                submit.click()
        
                # 直到获取到淘宝会员昵称才能确定是登录成功
                taobao_name = self.wait.until(EC.presence_of_element_located((By.CSS_SELECTOR, '.site-nav-bd > ul.site-nav-bd-l > li#J_SiteNavLogin > div.site-nav-menu-hd > div.site-nav-user > a.site-nav-login-info-nick ')))
                # 输出淘宝昵称
                print(taobao_name.text)
        
                self.browser.get('https://detail.1688.com/offer/607462826120.html?spm=a26e3.8027625.1999173159.1.4110540cWETUm1')
                print(self.browser.page_source)
        
        
        # 使用教程:
        # 1.下载chrome浏览器:https://www.google.com/chrome/
        # 2.查看chrome浏览器的版本号,下载对应版本号的chromedriver驱动:http://chromedriver.storage.googleapis.com/index.html
        # 3.填写chromedriver的绝对路径
        # 4.执行命令pip install selenium
        # 5.打开https://account.weibo.com/set/bindsns/bindtaobao并通过微博绑定淘宝账号密码
        
        if __name__ == "__main__":
            
        
            chromedriver_path = r"E:安装包chromedriver.exe" #改成你的chromedriver的完整路径地址
            weibo_username = "" #改成你的微博账号
            weibo_password = "" #改成你的微博密码
        
            a = taobao_infos()
            a.login() #登录
            
  • 相关阅读:
    phpexcel读取excel文件
    laravel的表单验证(下面有些信息未验证,转的)
    Laravel框架学习(Response)
    laravel文件存储Storage
    Laravel 5 教程
    js中substring和substr的用法
    jquery 规范
    总结oninput、onchange与onpropertychange事件的用法和区别
    jquery 选择器的总结
    jquery选择器中的find和空格,children和>的区别、及父节点兄弟节点,还有判断是否存在的写法
  • 原文地址:https://www.cnblogs.com/jxd283465/p/12008998.html
Copyright © 2011-2022 走看看