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()







  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值