ソースを参照

ADD:广告投放计划-出版

cxyu 4 年 前
コミット
37c4978e73

+ 1 - 0
selenium_master/__init__.py

@@ -0,0 +1 @@
+#通过http请求获取到selenium-docker引擎----存活时间一小时

+ 0 - 0
wechat_action/__init__.py


+ 12 - 0
wechat_action/create_ad.py

@@ -0,0 +1,12 @@
+#这个页面是广告计划投放
+
+
+#点击创建广告,切到广告页面
+    # gg_button=driver.find_element_by_css_selector('#wxadcontainer > div:nth-child(1) > div.weui-desktop-layout__main__bd > div.weui-desktop-panel > div.mod-infobox > div.mod-infobox__sub-head.ui-bt-none > div > div.ui-fl-r > button')
+    # gg_button.click()
+
+    #投放计划创建
+
+    #广告创建
+
+    #创意创建

+ 164 - 0
wechat_action/send_ad.py

@@ -0,0 +1,164 @@
+from selenium import webdriver
+from selenium.webdriver import ActionChains
+from selenium.webdriver.common.by import By
+from selenium.webdriver.support import expected_conditions as EC
+from selenium.webdriver.support.wait import WebDriverWait
+from wechat_action.send_ad_idea import IdeaAction
+from selenium.webdriver import ChromeOptions
+
+import time
+import random
+
+
+class SendAd():
+    # TODO:一旦登录之后,driver就不要关闭,然后错误就部分不断重试
+
+    def __init__(self, task):
+        self.task = task
+        self.task_check()
+        self.driver = self.get_driver()
+        self.ad_idea_action = IdeaAction(self.driver)
+
+    def get_driver(self):
+        options = ChromeOptions()
+        # 防止selenium快速崩坏
+        options.add_argument("--disable-dev-shm-usage")
+        # driver = webdriver.Remote(
+        #     command_executor='http://192.168.7.245:4444/wd/hub',
+        #     options=options)
+        driver=webdriver.Chrome()
+        return driver
+
+    def log_in(self):
+        self.driver.get('https://a.weixin.qq.com/index.html')
+        self.driver.save_screenshot('wechat_page_start.png')
+
+        img_selector = 'body > div.old-template > div > div > div.waiting.panelContent > div.wrp_code > img'
+        'body > div.old-template > div > div > div.waiting.panelContent > div.wrp_code > img'
+        frame_login = self.driver.find_element_by_xpath('//*[@id="login_container"]/iframe')
+        self.driver.switch_to.frame(frame_login)
+        # WebDriverWait(driver, 3).until(EC.invisibility_of_element_located((By.CSS_SELECTOR, img_selector)))
+        # time.sleep(3)
+        img_url = self.driver.find_element_by_css_selector(img_selector)
+        print('img', img_url.get_attribute('src'))
+        WebDriverWait(self.driver, 100).until(lambda driver: driver.find_elements_by_link_text('广告投放'))
+        self.driver.save_screenshot('get_in_page.png')
+
+    def select_ad_master(self):
+        # TODO:切换服务商
+
+
+        # 挑选广告投放位置
+        self.driver.execute_script('''
+            window.scroll(100000,1000000);
+            var e_one=document.getElementsByClassName('Table_new__wrapper-1cpZN')[0];
+            e_one.scroll(10000,100000);
+            ''')
+        elements = self.driver.find_elements_by_link_text('广告投放')
+        # ActionChains(driver).move_to_element(elements[0]).perform()
+        elements[1].click()
+        time.sleep(1)
+        # 切换窗口,点击创建广告,切到广告页面
+
+        self.driver.switch_to.window(self.driver.window_handles[-1])
+
+        WebDriverWait(self.driver, 100).until(lambda driver: driver.find_element_by_class_name(
+            'adui-button-hasLeftIcon'))
+
+        gg_button = self.driver.find_element_by_class_name(
+            'adui-button-hasLeftIcon')
+        gg_button.click()
+
+    def select_ad_locations(self):
+        # 设置广告形式
+        self.driver.switch_to.window(self.driver.window_handles[-1])
+        self.driver.execute_script('''
+                        window.scroll(100000,0);
+                        ''')
+        WebDriverWait(self.driver, 10).until(
+            lambda driver: self.driver.find_element_by_css_selector('#PRODUCTTYPE_WECHAT'))
+        # 推广公众号
+        wechat_gg = self.driver.find_element_by_css_selector('#PRODUCTTYPE_WECHAT')
+        wechat_gg.click()
+        localtion = self.task['localtion']
+        if localtion == 'gzh_bottom':
+            # 默认是这个选项,所以不用进行点选
+            pass
+        if localtion == 'gzh_movie':
+            wechat_localtion = self.driver.find_element_by_id('pos_8')
+            wechat_localtion.click()
+        if localtion == 'pyq':
+            wechat_localtion = self.driver.find_element_by_css_selector(
+                '#wxadcontainer > div:nth-child(1) > div > div.content-2-HFBij0Uh > main > div > div:nth-child(3) > div.adui-grid-row > div:nth-child(1)')
+            wechat_localtion.click()
+        time.sleep(random.uniform(0.1, 0.3))
+
+        # 花销设置
+        # cost_input = self.driver.find_element_by_css_selector(
+        #     '#wxadcontainer > div:nth-child(1) > div > div.content-2-HFBij0Uh > main > div > div:nth-child(5) > div:nth-child(2) > div:nth-child(1) > div > input')
+        # cost_input.send_keys('100')
+
+        # 进入下一页
+        next_button = self.driver.find_element_by_css_selector('#plan_next_step')
+        next_button.click()
+        self.driver.save_screenshot('set_page_localtion.png')
+
+    def set_ad_cost(self):
+        # 投放计划创建
+        # 广告创建
+        WebDriverWait(self.driver, 10).until(
+            lambda driver: self.driver.find_element_by_css_selector('#area_input > div > i'))
+        self.driver.find_element_by_css_selector('#area_input > div > i').click()
+        if self.task['localtion'] == 'gzh_botoom':
+            self.driver.find_element_by_class_name('icon-3cAquX1RLZ').click()
+        if self.task['localtion'] == 'gzh_movie':
+            citys = self.driver.find_elements_by_class_name('checkbox-1pfRMRqv-R')
+            for _ in citys:
+                _.click()
+        if self.task['localtion'] == 'pyq':
+            pass
+        day_cost = self.driver.find_element_by_css_selector('#budget_input')
+        day_cost.send_keys('100')
+        self.driver.save_screenshot('set_page_cost.png')
+        self.driver.find_element_by_css_selector('#target_next_step').click()
+
+    def set_ad_idea(self):
+        # TODO:实验一下视频上传
+        # TODO:创意创建这部分切割开
+        # 创意创建
+        if self.task['localtion'] == 'gzh_bottom':
+            self.ad_idea_action.idea_pic_gzh_bottom()
+        if self.task['localtion'] == 'gzh_movie':
+            self.ad_idea_action.idea_movie_gzh_movie()
+        if self.task['localtion'] == 'pyq':
+            pass
+
+        next_button = self.driver.find_element_by_class_name('adui-button-primary')
+        next_button.click()
+
+    def push_ad(self):
+        # 提交
+        time.sleep(random.uniform(3, 4))
+        self.driver.find_element_by_class_name('icon-3cAquX1RLZ').click()
+        time.sleep(random.uniform(0.5, 1))
+        self.driver.save_screenshot('push_page.png')
+        self.driver.find_element_by_class_name('adui-button-primary').click()
+
+    def task_check(self):
+        # TODO:检查任务有无输入问题,
+        if 'master' not in self.task.keys() or 'localtion' not in self.task.keys():
+            exit()
+
+    def run(self):
+        # TODO:出错就截图,然后保存
+        self.log_in()
+        self.select_ad_master()
+        self.select_ad_locations()
+        self.set_ad_cost()
+        self.set_ad_idea()
+        self.push_ad()
+
+
+if __name__ == '__main__':
+    task = {'master': '', 'localtion': 'gzh_movie'}
+    SendAd(task).run()

+ 54 - 0
wechat_action/send_ad_idea.py

@@ -0,0 +1,54 @@
+import time
+import random
+from selenium.webdriver.support.wait import WebDriverWait
+
+
+class IdeaAction:
+    def __init__(self, driver):
+        self.driver = driver
+
+    def push_page(self):
+        self.driver.find_element_by_class_name('adui-button-small').click()
+        select_input = self.driver.find_element_by_class_name('adui-input-base')
+        select_input.send_keys('原生')
+        self.driver.find_element_by_id('select_canvas_ok_btn').click()
+        time.sleep(random.uniform(1, 2))
+
+    def idea_banner_gzh_bottom(self):
+        self.driver.find_element_by_css_selector('#test_material_type_135 > span.inputWrapper-3fBModc9zM > i').click()
+        wen_input = self.driver.find_element_by_class_name('input-3MbKvywHL2')
+        wen_input.send_keys('nihaonihao')
+
+    def idea_pic_gzh_bottom(self):
+        # TODO:添加try,catch因为图片上传可能慢
+        WebDriverWait(self.driver, 10).until(lambda driver: driver.find_element_by_class_name('adui-button-small'))
+
+        self.driver.find_element_by_class_name("webuploader-element-invisible").send_keys(
+            "/usr/share/wallpapers/deepin/Beach_by_Samuel_Scrimshaw.jpg")
+        WebDriverWait(self.driver, 5).until(lambda driver: driver.find_element_by_class_name('btnFist-uueBS6DQFa'))
+        self.driver.find_element_by_class_name('btnFist-uueBS6DQFa').click()
+        WebDriverWait(self.driver, 10).until(lambda driver: driver.find_element_by_id('test_progress_confirm'))
+        # 等待上传完毕,因为完成按钮本身可以
+        time.sleep(10)
+        self.driver.find_element_by_id('test_progress_confirm').click()
+        time.sleep(random.uniform(1, 2))
+
+        self.driver.save_screenshot('set_page_pic.png')
+        self.push_page()
+        self.driver.save_screenshot('set_page_idea.png')
+
+    def idea_movie_gzh_movie(self):
+        WebDriverWait(self.driver, 10).until(
+            lambda driver: driver.find_element_by_class_name('webuploader-element-invisible'))
+
+        self.driver.find_element_by_class_name('webuploader-element-invisible').send_keys(
+            '/home/cxyu/Downloads/老奶奶.mp4')
+        suc_button = self.driver.find_element_by_id('test_progress_confirm')
+        WebDriverWait(self.driver, 1000).until(lambda  x:(suc_button.is_displayed() and suc_button.is_enabled()))
+
+        self.driver.find_element_by_id('test_progress_confirm').click()
+
+        self.driver.save_screenshot('set_page_pic.png')
+        self.push_page()
+        text_input = self.driver.find_element_by_css_selector('#reward_video_desc > div > input')
+        text_input.send_keys('asdf')