短信验证登录淘宝web页面
自动化模拟登录(chromedriver存在python环境里)
第一步进行导包
import time
from selenium.webdriver import ChromeOptions
from selenium.webdriver import ActionChains
from selenium import webdriver
from selenium.webdriver.common.by import By
第二步定位获取页面的web源代码
获取某一页的html(想获取那一页就放在跳转之后的代码下面
win = c.window_handles
time.sleep(2)
c.switch_to.window(win[0])
print(c.page_source)
第三步构造是以chrome浏览器登录而不是以selenium的自动化登录并进入登录页面(防止滑动验证的出现)
登录模块
def login():
# 模拟人为在浏览器登录,防止淘宝识别是selenium访问
option = ChromeOptions()
# 设置当前爬虫行为不是selenium自动框架
option.add_argument('--disable-blink-features=AutomationControlled')
# options=option是应用防识别selenium
c = webdriver.Chrome(executable_path="chromedriver", options=option)
c.get("https://www.taobao.com")
c.find_element(By.XPATH, '//*[@id="J_SiteNavLogin"]/div[1]/div[1]/a[1]').click()
loginButtom(c)
第四步登录淘宝
登录模块
def loginButtom(c):
username = '账号'
ps = '密码'
c.find_element(By.XPATH, '//*[@id="fm-login-id"]').send_keys(username)
c.find_element(By.XPATH, '//*[@id="fm-login-password"]').send_keys(ps)
c.find_element(By.XPATH, '//*[@id="login-form"]/div[4]/button').click()
win = c.window_handles
time.sleep(1)
c.switch_to.window(win[0])
if c.find_element(By.XPATH, '//*[@id="J_SiteNavLogin"]/div[1]/div/a').text == '你的账号名':
uesrMessage(c)
else:
messageCheck(c)
第五步点击以短信验证登录
def messageCheck(c):
iframe = c.find_element(By.XPATH, '//*[@id="content"]/div/div[1]/iframe')
c.switch_to.frame(iframe)
c.find_element(By.XPATH, '//*[@id="otherValidator"]').click()
c.find_element(By.XPATH, '//*[@id="content"]/div/ol/li[1]/a').click()
c.find_element(By.XPATH, '//*[@id="J_GetCode"]').click()
number = input("请输入短信验证码:")
c.find_element(By.XPATH, '//*[@id="J_Phone_Checkcode"]').send_keys(number)
c.find_element(By.XPATH, '//*[@id="submitBtn"]').click()
uesrMessage(c)
第五步点击运行代码
if __name__ == '__main__':
login()