使用selenium模擬登錄淘寶、新浪和知乎

若是直接使用selenium訪問淘寶、新浪和知乎這些網址。通常會識別出這是自動化測試工具,會有反制措施。
當開啓開發者模式後,就能夠繞過他們的檢測啦。

幾個站模擬登錄的套路都是差很少。
麻煩一點的是知乎,總是彈出驗證碼。這裏不提驗證碼的繞過,真的有大量的登陸獲取cookie的需求。鏈接打碼平臺應該是個不錯的選擇...

其實用selenium操做瀏覽器時儘可能模擬人的操做,就能夠減小驗證碼出現的概率了。
直接上代碼吧,註釋裏會說明邏輯:

淘寶:
from selenium.webdriver.chrome.options import Options
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


URL = 'https://login.taobao.com/'
USER = ''
PASSWORD = ''
chrome_options = Options()  #設置
#chrome_options.add_argument('--headless')  #瀏覽器不提供可視化界面
chrome_options.add_argument('--disable-gpu')  #規避bug

# 設置開發者模式啓動,該模式下webdriver屬性爲正常值   通常反爬比較好的網址都會根據這個反爬
chrome_options.add_experimental_option('excludeSwitches', ['enable-automation'])

driver = webdriver.Chrome(chrome_options=chrome_options)  #配置設置
wait = WebDriverWait(driver, 10)  #超時時長爲10s
driver.get(URL)  #請求網址

#選擇密碼登陸
login_click = wait.until(EC.presence_of_element_located((By.XPATH, '//i[@class="iconfont static"]')))
login_click.click()

#選擇微博登陸
weibo_click = wait.until(EC.presence_of_element_located((By.XPATH, '//a[@class="weibo-login"]')))
weibo_click.click()

#等待微博帳號輸入框出現
weibo_user = wait.until(EC.presence_of_element_located((By.CSS_SELECTOR, '.username > .W_input')))
weibo_user.send_keys(USER)

#等待微博密碼輸入框出現
weibo_pwd = wait.until(EC.presence_of_element_located((By.CSS_SELECTOR, '.password > .W_input')))
weibo_pwd.send_keys(PASSWORD)

#等待登陸按鈕出現
submit = wait.until(EC.presence_of_element_located((By.CSS_SELECTOR, '.btn_tip > a > span')))
submit.click()

#在搜索框中輸入搜索關鍵字
search_input = wait.until(EC.presence_of_element_located((By.XPATH, '//input[@id="q"]')))
search_input.send_keys('美食')

#driver.close()

 

 

知乎:html

from selenium.webdriver.chrome.options import Options
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
from selenium.webdriver.common.action_chains import ActionChains

import time

URL = 'https://www.zhihu.com/signin'
USER = ''
PASSWORD = ''
chrome_options = Options()  #設置
#chrome_options.add_argument('--headless')  #瀏覽器不提供可視化界面
chrome_options.add_argument('--disable-gpu')  #規避bug


# 設置開發者模式啓動,該模式下webdriver屬性爲正常值   通常反爬比較好的網址都會根據這個反爬
chrome_options.add_experimental_option('excludeSwitches', ['enable-automation'])

driver = webdriver.Chrome(chrome_options=chrome_options)
driver.maximize_window()  #全屏打開瀏覽器
wait = WebDriverWait(driver, 10) #超時時長爲10s

driver.get(URL)

#轉到密碼登陸
change = wait.until(EC.presence_of_element_located((By.XPATH, '//div[@class="SignFlow-tab"]')))
change.click()
# 等待知乎帳號輸入框出現
zhihu_user = wait.until(EC.presence_of_element_located((By.XPATH, '//input[@name="username"]')))
zhihu_user.click()
time.sleep(1)
zhihu_user.send_keys(USER)

# 等待知乎密碼輸入框出現
zhihu_pwd = wait.until(EC.presence_of_element_located((By.XPATH, '//input[@name="password"]')))
zhihu_pwd.click()
zhihu_pwd.send_keys(PASSWORD)

time.sleep(1.5)
#直接點擊登陸按鈕
ActionChains(driver).move_by_offset(930, 500).click().perform() # 鼠標左鍵點擊, 200爲x座標, 100爲y座標

#driver.close()

 

 

新浪微博:web

from selenium.webdriver.chrome.options import Options
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
from selenium.webdriver.common.action_chains import ActionChains

import time

URL = 'https://weibo.com'
USER = ''
PASSWORD = ''
chrome_options = Options()  #設置
#chrome_options.add_argument('--headless')  #瀏覽器不提供可視化界面
chrome_options.add_argument('--disable-gpu')  #規避bug

# 設置開發者模式啓動,該模式下webdriver屬性爲正常值   通常反爬比較好的網址都會根據這個反爬
chrome_options.add_experimental_option('excludeSwitches', ['enable-automation'])

driver = webdriver.Chrome(chrome_options=chrome_options)  #配置設置
driver.maximize_window()  #全屏打開瀏覽器
wait = WebDriverWait(driver, 10) #超時時長爲10s

driver.get(URL)

# 等待微博帳號輸入框出現
weibo_user = wait.until(EC.presence_of_element_located((By.XPATH, '//input[@id="loginname"]')))
weibo_user.click()  # 用來模擬手工點擊一下再輸入帳號
time.sleep(0.5)  # 延時一下,速度太快好像會致使驗證碼的出現
weibo_user.send_keys(USER)  #輸入帳號

# 等待微博密碼輸入框出現
weibo_pwd = wait.until(EC.presence_of_element_located((By.XPATH, '//div[@class="input_wrap"]/input[@name="password"]')))
weibo_pwd.send_keys(PASSWORD)

# 直接按座標點擊登陸按鈕
ActionChains(driver).move_by_offset(1360, 280).click().perform() # 鼠標左鍵點擊

#driver.close()

 

 

參考:http://www.javashuo.com/article/p-nqhdjcbq-t.html

 

The end~chrome

相關文章
相關標籤/搜索