【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() #登录
        

 

posted @ 2019-12-09 08:55    阅读(1680)  评论(0编辑  收藏  举报