栏目分类:
子分类:
返回
文库吧用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
文库吧 > IT > 软件开发 > 后端开发 > Python

LAZADA推广统计

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

LAZADA推广统计

#!/usr/bin/python3
# -- coding: UTF-8 --
from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
from selenium.common.exceptions import NoSuchElementException
from selenium.common.exceptions import ElementClickInterceptedException
import time
import xlrd
import xlwings as xw
import pyautogui
import csv

options = webdriver.ChromeOptions()
# 此步骤很重要,设置为开发者模式,防止被各大网站识别出来使用了Selenium
options.add_experimental_option('excludeSwitches', ['enable-automation'])
options.add_argument('--ignore-certificate-errors')
driver = webdriver.Chrome(options=options)
options = webdriver.ChromeOptions()
chains = ActionChains(driver)

driver.set_window_size(1500, 1000)

login_url = "https://gsp.lazada-seller.cn/page/login"


# 判断语句
def isElementExist(self):  # xpath
    # print(self)
    flag = True
    # print(flag)
    try:
        driver.find_element_by_xpath(self)
        # print(driver.find_element_by_css_selector(self))
        return flag
    except:
        flag = False
        return flag


def id_exist(self):
    # print(self)
    flag = True
    # print(flag)
    try:
        driver.find_element_by_id(self)
        # print(driver.find_element_by_css_selector(self))
        return flag
    except:
        flag = False
        return flag


def try_login(username, password):
    # 找到用户名输入框,输入账号
    login_name = driver.find_element_by_id("fm-login-id")
    # 输入账号
    login_name.send_keys(username)
    time.sleep(4)
    # 找到密码输入框,输入密码
    login_password = driver.find_element_by_id("fm-login-password")
    # 输入密码
    login_password.send_keys(password)

    # 找到登陆框
    login_button_1 = driver.find_element_by_id("fm-login-submit")
    time.sleep(2)
    # 通过链的方式模拟登陆
    login_button_1.click()
    time.sleep(3)

    # 刷新一下
    driver.refresh()
    time.sleep(2)


# 找到活动页
def try_promotions():
    chains.move_to_element(driver.find_element_by_xpath("//div/div[2]/div/div/div/div[6]/div/div[2]/section/div/div[2]/div[3]/div[2]/div[2]/table/tbody/tr/td[2]/div/div/div/div/div[1]/a").click()
        time.sleep(2)
    except NoSuchElementException:
        return

    # 点击时间轴
    try:
        driver.find_element_by_xpath(
            "//div[2]/span/input").click()
        time.sleep(5)
    except NoSuchElementException:
        return

    # 点击过去30天  7月1号 -7月 31号
    try:
        # 向上一个月
        driver.find_element_by_xpath(
            "//div/div[2]/div/div/div/div[6]/div/div[2]/section/div/div[2]/div[3]/div[2]/div[2]/table/tbody/tr/td[2]/div/div/div/div/div[1]/a").click()
        time.sleep(2)
    except NoSuchElementException:
        return

    # 点击时间轴
    try:
        driver.find_element_by_xpath(
            "//div[2]/span/input").click()
        time.sleep(5)
    except NoSuchElementException:
        return

    # 点击过去30天  7月1号 -7月 31号
    try:
        # 向上一个月
        driver.find_element_by_xpath(
            "//div/div[2]/div/div/div/div[6]/div/div[2]/section/div/div[2]/div[3]/div[2]/div[2]/table/tbody/tr/td[2]/div/div/div/div/div[1]/a").click()
        time.sleep(2)
    except NoSuchElementException:
        return

    # 点击时间轴
    try:
        driver.find_element_by_xpath(
            "//div[2]/span/input").click()
        time.sleep(5)
    except NoSuchElementException:
        return

    # 点击过去30天  7月1号 -7月 31号
    try:
        # 向上一个月
        driver.find_element_by_xpath(
            "//*[@id='product-ads']/div[4]/div/div/div/div/div[1]/div[1]/div[2]/div[1]/div/a[2]").click()
        time.sleep(2)
        driver.find_element_by_xpath(
            "//*[@id='product-ads']/div[4]/div/div/div/div/div[1]/div[1]/div[2]/div[2]/table/tbody/tr[1]/td[6]/div").click()
        time.sleep(2)
        driver.find_element_by_xpath(
            "//*[@id='product-ads']/div[4]/div/div/div/div/div[1]/div[1]/div[2]/div[2]/table/tbody/tr[6]/td[1]/div").click()
        time.sleep(2)
    except NoSuchElementException:
        return

    spend = driver.find_elements_by_xpath(
        "//*[@id='product-ads']/div[2]/div[2]/div[3]/div/div[2]/div[1]/div[2]/div/span[2]")
    Impressions = driver.find_elements_by_xpath(
        "//*[@id='product-ads']/div[2]/div[2]/div[3]/div/div[2]/div[2]/div[2]/div/span[2]")
    Clicks = driver.find_elements_by_xpath(
        "//*[@id='product-ads']/div[2]/div[2]/div[3]/div/div[2]/div[3]/div[2]/div/span[2]")
    CTR = driver.find_elements_by_xpath(
        "//*[@id='product-ads']/div[2]/div[2]/div[3]/div/div[2]/div[4]/div[2]/div/span[1]")
    CPC = driver.find_elements_by_xpath(
        "//*[@id='product-ads']/div[2]/div[2]/div[3]/div/div[2]/div[5]/div[2]/div/span[2]")
    StoreUnitsSold = driver.find_elements_by_xpath(
        "//*[@id='product-ads']/div[2]/div[2]/div[3]/div/div[2]/div[6]/div[2]/div/span[2]")
    StoreRevenue = driver.find_elements_by_xpath(
        "//*[@id='product-ads']/div[2]/div[2]/div[3]/div/div[2]/div[7]/div[2]/div/span[2]")
    StoreROI = driver.find_elements_by_xpath(
        "//*[@id='product-ads']/div[2]/div[2]/div[3]/div/div[2]/div[8]/div[2]/div/span[2]")

    spend_1 = spend[0].text.replace('VND', '')
    Impressions_1 = Impressions[0].text
    Clicks_1 = Clicks[0].text
    CTR_1 = CTR[0].text
    CPC_1 = CPC[0].text
    StoreUnitsSold_1 = StoreUnitsSold[0].text
    StoreRevenue_1 = StoreRevenue[0].text.replace('VND', '')
    StoreROI_1 = StoreROI[0].text

    datas = []
    item = {}
    item['spend'] = spend_1
    item['Impressions'] = Impressions_1
    item['Clicks'] = Clicks_1
    item['CTR'] = CTR_1
    item['CPC'] = CPC_1
    item['StoreUnitsSold'] = StoreUnitsSold_1
    item['StoreRevenue'] = StoreRevenue_1
    item['StoreROI'] = StoreROI_1
    item['username'] = username
    # dic = item
    datas.append(item)
    print(datas)

    header = ['username', 'spend', 'Impressions', 'Clicks', 'CTR', 'CPC', 'StoreUnitsSold', 'StoreRevenue', 'StoreROI']
    with open('test0801.csv', 'a', newline='', encoding='utf-8') as f:
        writer = csv.DictWriter(f, fieldnames=header)  # 提前预览列名,当下面代码写入数据时,会将其一一对应。
        writer.writeheader()  # 写入列名
        writer.writerows(datas)  # 写入数据


# 登入页面
def login_pass(username, password):
    driver.get(url=login_url)
    # time.sleep属于强制等待,模拟人的等待行为
    time.sleep(3)

    count = 3
    while count:
        # 登入
        flag = id_exist("fm-login-id")
        if flag:
            try_login(username, password)
        # print('第%s次尝试' % i)
        # i = i + 1
        # 判断语句
        else:
            count -= 1
            i = 3 - count
            print('%s 登入失败' % username)
            print('第%s尝试失败,总计3次' % i)
            continue

    time.sleep(3)
    flag = isElementExist("//*[contains(text(),'Promotions')]")
    for i in range(1, 3):
        if flag:
            print('%s 登入成功' % username)
            # 找到活动页
            try_promotions()
            break
        else:
            print("等待活动页")
        # 切换到活动页
    time.sleep(5)
    # driver.close()
    for handle in driver.window_handles:
        # 先切换到该窗口
        driver.switch_to.window(handle)
        # 得到该窗口的标题栏字符串,判断是不是我们要操作的那个窗口
        if 'Seller Center' in driver.title:
            # 如果是,那么这时候WebDriver对象就是对应的该该窗口,正好,跳出循环,
            break
        # print(driver.title)
        # page=driver.page_source
        # print(page)
        # dig_alert = driver.switch_to_alert()
        # print(dig_alert.text)
        # alert.dismiss()
        # div类型弹窗
        ##################################################

    ############### 每次活动都不一样?###########
    # 下滑一次
    # driver.execute_script("window.scrollBy(0,300)")
    # time.sleep(3)
    # special invitation

    ### MY 推广 ###
    for j in range(1, 3):
        x_exist()  # 判断是否有其他窗口

    '''
    time.sleep(2)
    driver.execute_script("window.scrollBy(0,800)")
    time.sleep(2)

    flag = isElementExist("//table/tbody/tr/td[2]/div/div/div/div/div[1]/a")
    if flag:
        MY_promotion(username)
    else:
        print('%s MY站无推广计划' % username)

    
    ###切换站点处理/ Philippine ######

    try:
        driver.find_element_by_xpath("//span[contains(text(),'Philippine')]").click()
        time.sleep(2)
    except NoSuchElementException:
        return
    ##########################

    for j in range(1, 3):
        x_exist()  # 判断是否有其他窗口

    try:
        # Management
        driver.find_element_by_xpath("//*[@id='product-ads']/div[2]/div/div/div[1]/div/div[2]/div[1]/div[2]/div[3]/span/button/span").click()
        time.sleep(2)
    except NoSuchElementException:
        print('无推广计划')
        return

    time.sleep(2)
    driver.execute_script("window.scrollBy(0,800)")
    time.sleep(2)

    flag = isElementExist("//table/tbody/tr/td[2]/div/div/div/div/div[1]/a")
    if flag:
        PH_promotion(username)
    else:
        print('%s PH站无活动页面')

    
    ###切换站点处理/ Singapore ######
    driver.find_element_by_xpath("//span[contains(text(),'Singapore')]").click()
    time.sleep(2)
    ##########################
    for j in range(1, 3):
        x_exist()  # 判断第一个X键

    flag = isElementExist("//h4[contains(text(),'May 2022 - April 2023 LazGlobal International Day BAU Campaign')]")
    if flag:
        SG_promotion()
    else:
        print('%s SG站无活动页面')
        
    
    ###切换站点处理/ Thailand ######
    driver.find_element_by_xpath("//span[contains(text(),'Thailand')]").click()
    time.sleep(2)
    ##########################

    for j in range(1, 3):
        x_exist()  # 判断是否有其他窗口

    try:
        # Management
        driver.find_element_by_xpath("//*[@id='product-ads']/div[2]/div/div/div[1]/div/div[2]/div[1]/div[2]/div[3]/span/button/span").click()
        time.sleep(2)
    except NoSuchElementException:
        print('无推广计划')
        return

    time.sleep(2)
    driver.execute_script("window.scrollBy(0,1200)")
    time.sleep(2)

    flag = isElementExist("//table/tbody/tr/td[2]/div/div/div/div/div[1]/a")
    if flag:
        TH_promotion(username)
    else:
        print('%s TH站无活动页面')

    '''
    ###切换站点处理/ Vietnam ######
    try:
        driver.find_element_by_xpath("//span[contains(text(),'Vietnam')]").click()
        time.sleep(2)
    except NoSuchElementException:
        return
    ##########################
    for j in range(1, 3):
        x_exist()  # 判断是否有其他窗口

    try:
        # Management
        driver.find_element_by_xpath("//*[@id='product-ads']/div[2]/div/div/div[1]/div/div[2]/div[1]/div[2]/div[3]/span/button/span").click()
        time.sleep(2)
    except NoSuchElementException:
        print('无推广计划')
        return

    time.sleep(2)
    driver.execute_script("window.scrollBy(0,1200)")
    time.sleep(2)

    flag = isElementExist("//table/tbody/tr/td[2]/div/div/div/div/div[1]/a")
    if flag:
        VN_promotion(username)
    else:
        print('%s VN站无活动页面')



# 分站点 读取文件,写入文件,提交报名部分

def login(url):
    # 循环账号
    work_book = xlrd.open_workbook('usename.xls')
    sheet_1 = work_book.sheet_by_index(0)
    for i in range(1, 90):
        global usename
        try:
            username = sheet_1.cell_value(i, 0)
        except IndexError:
            print('list index out of range')
            return
        password = sheet_1.cell_value(i, 1)
        # print(username)
        # print(password)
        # 第一次登入
        login_pass(username, password)
        time.sleep(3)
        driver.get(url=login_url)
        time.sleep(20)

    driver.quit()


def main():
    login(login_url)
    # login()


if __name__ == '__main__':
    main()

转载请注明:文章转载自 www.wk8.com.cn
本文地址:https://www.wk8.com.cn/it/1037996.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 wk8.com.cn

ICP备案号:晋ICP备2021003244-6号