用python的seleniumy自動登入pixiv搜尋關鍵字
本來想做用python自動爬取pixiv圖片,但pixiv有防爬蟲要用requests,所以先把用selenium寫得傳出來.
import time
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.keys import Key
path = "C:/Users/Administrator/Desktop/chromedriver/chromedriver.exe"
driver = webdriver.Chrome(path)
def pixiv(name, key, keyword):
driver.get("https://www.pixiv.net/")
login = driver.find_element_by_xpath('//*[@id="wrapper"]/div[3]/div[3]/div/div[2]/a[4]')
time.sleep(1)
login.click()
time.sleep(1)
login_two = WebDriverWait(driver, 10).until(
EC.presence_of_element_located((By.XPATH, '//*[@id="loginbutton"]'))
)
username = WebDriverWait(driver, 10).until(
EC.presence_of_element_located((By.XPATH, '//*[@id="email"]'))
)
passkey = WebDriverWait(driver, 10).until(
EC.presence_of_element_located((By.XPATH, '//*[@id="pass"]'))
)
username.clear()
passkey.clear()
username.send_keys(name)
passkey.send_keys(key)
login_two.click()
WebDriverWait(driver, 10).until(
EC.presence_of_element_located((By.XPATH, '//*[@id="root"]/div[2]/div[1]/div[1]/div[1]/div/div[2]/form/div/input'))
)
sreach = driver.find_element_by_xpath('//*[@id="root"]/div[2]/div[1]/div[1]/div[1]/div/div[2]/form/div/input')
sreach.send_keys(keyword)
sreach.send_keys(Keys.RETURN)
首先要用到selenium, time
要先載入這兩個模組.
要使用selenium要先下載你使用的瀏覽器對應的driver
我用的是google所以我下載chromedriver(https://chromedriver.chromium.org/)
把chromedriver的路徑放入一個變數
path = "C:/Users/Administrator/Desktop/chromedriver/chromedriver.exe"
設定用chrome打開
driver = webdriver.Chrome(path)
開啟pixiv
driver.get("https://www.pixiv.net/")
選用facebook開啟
login = driver.find_element_by_xpath('//*[@id="wrapper"]/div[3]/div[3]/div/div[2]/a[4]')
點下按鈕
login.click()
停一秒
time.sleep(1)
再點下後會有頁面跳轉,所以用webdriverwait,直到出現帳號密碼才繼續,其實用time.sleep也可以
login_two = WebDriverWait(driver, 10).until(
EC.presence_of_element_located((By.XPATH, '//*[@id="loginbutton"]'))
)
username = WebDriverWait(driver, 10).until(
EC.presence_of_element_located((By.XPATH, '//*[@id="email"]'))
)
webdriverwait會回傳一個質,所以用clear()清除
username.clear()
passkey.clear()
輸入帳密
username.send_keys(name)
passkey.send_keys(key)
找到搜索框
sreach = driver.find_element_by_xpath('//*[@id="root"]/div[2]/div[1]/div[1]/div[1]/div/div[2]/form/div/input')
輸入關鍵字
sreach.send_keys(keyword)
按enter,Key是引入進來的函數,在send_keys中輸入Keys.RETURN就是按下enter
sreach.send_keys(Keys.RETURN)
之後就只要呼叫函式就可以了
函式中的name, key, keyword需打成字串,不然會錯誤