某某登录的两种方法
方法一:微博登录
1 from selenium import webdriver
2 from selenium.webdriver.common.by import By
3 from selenium.webdriver.support.ui import WebDriverWait
4 from selenium.webdriver.support import expected_conditions as EC
5
6
7 # 定义一个taobao类
8 class taobao_infos:
9
10 # 对象初始化
11 def __init__(self):
12 url = 'https://login.taobao.com/member/login.jhtml'
13 self.url = url
14
15 options = webdriver.ChromeOptions()
16 options.add_experimental_option("prefs", {"profile.managed_default_content_settings.images": 2}) # 不加载图片,加快访问速度
17 options.add_experimental_option('excludeSwitches',
18 ['enable-automation']) # 此步骤很重要,设置为开发者模式,防止被各大网站识别出来使用了Selenium
19
20 self.browser = webdriver.Chrome(options=options)
21 self.wait = WebDriverWait(self.browser, 10) # 超时时长为10s
22
23 # 登录淘宝
24 def login(self):
25 # 打开网页
26 self.browser.get(self.url)
27
28 # 等待 密码登录选项 出现
29 password_login = self.wait.until(
30 EC.presence_of_element_located((By.CSS_SELECTOR, '.qrcode-login > .login-links > .forget-pwd')))
31 password_login.click()
32
33 # 等待 微博登录选项 出现
34 weibo_login = self.wait.until(EC.presence_of_element_located((By.CSS_SELECTOR, '.weibo-login')))
35 weibo_login.click()
36
37 # 等待 微博账号 出现
38 weibo_user = self.wait.until(EC.presence_of_element_located((By.CSS_SELECTOR, '.username > .W_input')))
39 weibo_user.send_keys(weibo_username)
40
41 # 等待 微博密码 出现
42 weibo_pwd = self.wait.until(EC.presence_of_element_located((By.CSS_SELECTOR, '.password > .W_input')))
43 weibo_pwd.send_keys(weibo_password)
44
45 # 等待 登录按钮 出现
46 submit = self.wait.until(EC.presence_of_element_located((By.CSS_SELECTOR, '.btn_tip > a > span')))
47 submit.click()
48
49 # 直到获取到淘宝会员昵称才能确定是登录成功
50 taobao_name = self.wait.until(EC.presence_of_element_located((By.CSS_SELECTOR,
51 '.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 ')))
52 # 输出淘宝昵称
53 print(taobao_name.text)
54
55
56 if __name__ == "__main__":
57 # chromedriver_path = "/Users/bird/Desktop/chromedriver.exe" # 改成你的chromedriver的完整路径地址
58 weibo_username = "" # 改成你的微博账号
59 weibo_password = "" # 改成你的微博密码
60
61 a = taobao_infos()
62 a.login() # 登录
方法二:保存cookie
先保存cookie
import json
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
options = Options()
dirver = webdriver.Chrome(options=options)
dirver.get(
'https://login.taobao.com/member/login.jhtml')
# 这里是为了等待手机扫码登录, 登录后回车即可
input("请回车登录")
dictCookies = dirver.get_cookies()
jsonCookies = json.dumps(dictCookies)
# 登录完成后,将cookies保存到本地文件
with open("./cookies_tao.json", "w") as fp:
fp.write(jsonCookies)
再次登录无需验证
1 import json
2 import time
3 from selenium import webdriver
4 from selenium.webdriver.chrome.options import Options
5
6 options = Options()
7 options.add_argument("--headless")
8 dirver = webdriver.Chrome(options=options)
9 # 初次建立连接, 随后方可修改cookie
10 dirver.get('http://www.taobao.com')
11 # 删除第一次登录是储存到本地的cookie
12 dirver.delete_all_cookies()
13 # 读取登录时储存到本地的cookie
14 with open("cookies_tao.json", "r", encoding="utf8") as fp:
15 ListCookies = json.loads(fp.read())
16
17 for cookie in ListCookies:
18 dirver.add_cookie({
19 'domain': '.taobao.com', # 此处xxx.com前,需要带点
20 'name': cookie['name'],
21 'value': cookie['value'],
22 'path': '/',
23 'expires': None
24 })
25
26 # 再次访问页面,便可实现免登陆访问
27 dirver.get("https://www.taobao.com")
28 time.sleep(3)
29 # 将页面保存为图片用于查看是否登录成功
30 dirver.save_screenshot("./taobao.png")
31 dirver.quit()