python + selenium登陸並點擊百度平臺

from PIL import Image
from selenium.webdriver import DesiredCapabilities
from selenium import webdriver
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
import time


class MsspBqt(object):
    def __init__(self):
        self.url = 'https://mssp.baidu.com/bqt#/'
        self.opt = webdriver.ChromeOptions()
        capa = DesiredCapabilities.CHROME
        capa['pageLoadStrategy'] = 'none'  # 懶加載模式,不等待頁面加載完畢
        # self.opt.set_headless()
        # 設置隨機請求頭
        self.opt.add_argument('User-Agent=Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:68.0) Gecko/20100101 Firefox/68.0')
        self.driver = webdriver.Chrome(desired_capabilities=capa)
        self.driver.maximize_window()
        self.wait = WebDriverWait(self.driver, 20)

    def login(self):
        self.driver.get(self.url)
        # 等待登陸按鈕元素加載出來
        self.wait.until(EC.presence_of_element_located((By.CLASS_NAME, 'btn-login')))
        # 點擊登陸按鈕
        self.driver.find_element_by_class_name('btn-login').click()
        time.sleep(0.2)
        # 輸入賬號密碼和驗證碼
        self.driver.find_element_by_id('username').send_keys('賬號')
        self.driver.find_element_by_id('password').send_keys('密碼')
        self.getCaptcha()
        captcha = input("請輸入驗證碼:")
        self.driver.find_element_by_id('imagecode').send_keys(captcha)
        self.driver.find_element_by_xpath('//div[@class="login-form-btn login-form-group"]/button').click()
        time.sleep(5)
        self.creat()

    def creat(self):
        for i in range(31):
            if i == 0:
                continue
            self.driver.get('https://mssp.baidu.com/bqt/websiteco.html#/union/slot/create')
            time.sleep(3)
            self.wait.until(EC.presence_of_element_located((By.XPATH, '//section/div/div/label/input[@class="veui-input-input"]')))
            self.driver.execute_script('window.stop();') # 停止當前頁面加載,防止input框輸入錯誤
            self.driver.find_element_by_xpath('//section/div/div/label/input[@class="veui-input-input"]').clear()
            self.driver.find_element_by_xpath('//section/div/div/label/input[@class="veui-input-input"]').send_keys('---<'+str(i)+'>')
            # 投放平臺點擊PC
            self.driver.find_element_by_xpath('//section[2]/div/div/button[@class="veui-button"]').click()
            if i % 2:
                print('奇數:', i)
                self.driver.find_element_by_xpath(
                    '//form/div[@class="form-operation"]/button[@class="veui-button"][1]').click()
            else:
                print('偶數:', i)
                # 圖文廣告
                self.driver.find_element_by_xpath('//section[3]/div/div/button[@class="veui-button"]').click()
                # 確定按鈕
                self.driver.find_element_by_xpath('//form/div[@class="form-operation"]/button[@class="veui-button"][1]').click()
            # 等待新建代碼位按鈕加載出來
            self.wait.until(EC.presence_of_element_located((By.XPATH, '//form/div/button[@class="veui-button"]')))

    def getCaptcha(self):
        self.driver.save_screenshot('current_page.png')
        # 驗證碼的座標位置
        left = 1876
        top = 271
        right = 1970
        bottom = 326
        img = Image.open('current_page.png')
        img = img.crop((left, top, right, bottom))
        img.save('captcha.png')


if __name__ == "__main__":
    bqt = MsspBqt()
    bqt.login()







發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章