send_ad.py 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. from selenium import webdriver
  2. from selenium.webdriver import ActionChains
  3. from selenium.webdriver.common.by import By
  4. from selenium.webdriver.support import expected_conditions as EC
  5. from selenium.webdriver.support.wait import WebDriverWait
  6. from wechat_action.send_ad_idea import IdeaAction
  7. from wechat_action.human_ad import HumanAd
  8. from wechat_action.login_ad import LogIn
  9. from selenium.webdriver import ChromeOptions
  10. import time
  11. import random
  12. class SendAd():
  13. # TODO:一旦登录之后,driver就不要关闭,然后错误就部分不断重试
  14. def __init__(self, task):
  15. self.task = task
  16. self.task_check()
  17. self.driver = LogIn().get_driver_loged()
  18. self.ad_idea_action = IdeaAction(self.driver)
  19. self.ad_human_action = HumanAd(self.driver)
  20. def select_ad_locations(self):
  21. gg_button = self.driver.find_element_by_class_name(
  22. 'adui-button-hasLeftIcon')
  23. gg_button.click()
  24. # 设置广告形式
  25. self.driver.switch_to.window(self.driver.window_handles[-1])
  26. self.driver.execute_script('''
  27. window.scroll(100000,0);
  28. ''')
  29. WebDriverWait(self.driver, 10).until(
  30. lambda driver: self.driver.find_element_by_css_selector('#PRODUCTTYPE_WECHAT'))
  31. # 推广公众号
  32. wechat_gg = self.driver.find_element_by_css_selector('#PRODUCTTYPE_WECHAT')
  33. wechat_gg.click()
  34. localtion = self.task['localtion']
  35. if localtion == 'gzh_bottom':
  36. # 默认是这个选项,所以不用进行点选
  37. pass
  38. if localtion == 'gzh_movie':
  39. wechat_localtion = self.driver.find_element_by_id('pos_8')
  40. wechat_localtion.click()
  41. if localtion == 'pyq':
  42. wechat_localtion = self.driver.find_element_by_css_selector(
  43. '#wxadcontainer > div:nth-child(1) > div > div.content-2-HFBij0Uh > main > div > div:nth-child(3) > div.adui-grid-row > div:nth-child(1)')
  44. wechat_localtion.click()
  45. time.sleep(random.uniform(0.1, 0.3))
  46. # 花销设置
  47. # cost_input = self.driver.find_element_by_css_selector(
  48. # '#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')
  49. # cost_input.send_keys('100')
  50. # 进入下一页
  51. next_button = self.driver.find_element_by_css_selector('#plan_next_step')
  52. next_button.click()
  53. self.driver.save_screenshot('set_page_localtion.png')
  54. def set_ad_cost(self):
  55. # 投放计划创建
  56. # 广告创建
  57. WebDriverWait(self.driver, 10).until(
  58. lambda driver: self.driver.find_element_by_css_selector('#area_input > div > i'))
  59. self.driver.find_element_by_css_selector('#area_input > div > i').click()
  60. if self.task['localtion'] == 'gzh_botoom':
  61. self.driver.find_element_by_class_name('icon-3cAquX1RLZ').click()
  62. if self.task['localtion'] == 'gzh_movie':
  63. citys = self.driver.find_elements_by_class_name('checkbox-1pfRMRqv-R')
  64. for _ in citys:
  65. _.click()
  66. if self.task['localtion'] == 'pyq':
  67. pass
  68. day_cost = self.driver.find_element_by_css_selector('#budget_input')
  69. day_cost.send_keys('100')
  70. self.driver.save_screenshot('set_page_cost.png')
  71. self.driver.find_element_by_css_selector('#target_next_step').click()
  72. def set_ad_human(self):
  73. #TODO:设置人群
  74. pass
  75. def set_ad_idea(self):
  76. # TODO:创意创建这部分切割开
  77. # 创意创建
  78. if self.task['localtion'] == 'gzh_bottom':
  79. self.ad_idea_action.idea_pic_gzh_bottom()
  80. if self.task['localtion'] == 'gzh_movie':
  81. self.ad_idea_action.idea_movie_gzh_movie()
  82. if self.task['localtion'] == 'pyq':
  83. pass
  84. next_button = self.driver.find_element_by_class_name('adui-button-primary')
  85. next_button.click()
  86. def push_ad(self):
  87. # 提交
  88. time.sleep(random.uniform(3, 4))
  89. self.driver.find_element_by_class_name('icon-3cAquX1RLZ').click()
  90. time.sleep(random.uniform(0.5, 1))
  91. self.driver.save_screenshot('push_page.png')
  92. self.driver.find_element_by_class_name('adui-button-primary').click()
  93. def task_check(self):
  94. # TODO:检查任务有无输入问题,
  95. if 'master' not in self.task.keys() or 'localtion' not in self.task.keys():
  96. exit()
  97. def run(self):
  98. # TODO:出错就截图,然后保存
  99. self.ad_human_action.check_human_info()
  100. self.select_ad_locations()
  101. self.set_ad_cost()
  102. self.set_ad_idea()
  103. self.push_ad()
  104. if __name__ == '__main__':
  105. task = {'master': '', 'localtion': 'gzh_movie'}
  106. SendAd(task).run()