cel_seo_click.py 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. import time
  2. import traceback
  3. import dataset
  4. from selenium import webdriver
  5. from selenium.webdriver.support.ui import WebDriverWait
  6. from selenium.webdriver.common.by import By
  7. from selenium.webdriver.support import expected_conditions as EC
  8. from selenium.webdriver.common.keys import Keys
  9. from celery import Celery
  10. import redis
  11. from celery.schedules import crontab
  12. import celery
  13. from celery.signals import celeryd_init
  14. from celery import signals
  15. import random
  16. from click import Option
  17. from selenium import webdriver
  18. import time
  19. app = Celery('tasks', backend ='redis://172.104.92.245', broker='redis://172.104.92.245')
  20. qname=None
  21. custname=None
  22. app.user_options['preload'].add(Option(('-Z', '--cust'),
  23. default='default',
  24. help='Configuration template to use.'))
  25. @signals.user_preload_options.connect
  26. def on_preload_parsed(options, **kwargs):
  27. global custname
  28. # if options.get_key('template')
  29. print(options['cust'])
  30. custname=options['cust']
  31. # print(options)
  32. @celeryd_init.connect
  33. def configure_workers(sender=None, **kwargs):
  34. global qname
  35. worker_name = sender.split("@")[-1]
  36. #rkey=app.request.delivery_info['routing_key']
  37. print("******")
  38. print(worker_name)
  39. qname=worker_name
  40. print("******")
  41. app.conf.beat_schedule = {
  42. # Executes every Monday morning at 7:30 a.m.
  43. 'add-every-monday-morning': {
  44. 'task': 'cel_seo_click.selenium_jared_click',
  45. # 'schedule': crontab(minute='*/1'),
  46. 'schedule': 100.0,
  47. 'args': (),
  48. 'options':{'queue': qname}
  49. },
  50. }
  51. #@app.on_after_configure.connect
  52. #def setup_periodic_tasks(sender, **kwargs):
  53. # print(app.request.delivery_info['routing_key'])
  54. # print(json_str)
  55. #sender task.request.hostnam
  56. # sender.add_periodic_task(100.0, selenium_jared_click.s(), name='add every 2 min')
  57. @app.task()
  58. def selenium_jared_click():
  59. global custname
  60. db = dataset.connect('postgresql://postgres:eyJhbGciOiJI@172.105.241.163:5432/postgres')
  61. options = webdriver.ChromeOptions()
  62. options.add_argument("--no-sandbox")
  63. options.add_argument("--headless")
  64. options.add_argument("--incognito")
  65. #options.add_argument('--blink-settings=imagesEnabled=false')
  66. r=random.randint(0,4)
  67. if r==0:
  68. PROXY = "172.105.205.52:8118" # IP:PORT or HOST:PORT
  69. print("PROXY:"+PROXY)
  70. elif r==1:
  71. PROXY = "139.144.120.184:8118" # IP:PORT or HOST:PORT
  72. print("PROXY:"+PROXY)
  73. elif r==2:
  74. PROXY = "172.104.92.245:8118" # IP:PORT or HOST:PORT
  75. print("PROXY:"+PROXY)
  76. elif r==3:
  77. PROXY = "172.104.67.159:8118" # IP:PORT or HOST:PORT
  78. print("PROXY:"+PROXY)
  79. else:
  80. PROXY=None
  81. if PROXY is not None:
  82. options.add_argument('--proxy-server=http://%s' % PROXY)
  83. mobile_emulation = {
  84. "deviceMetrics": { "width": 360, "height": 640, "pixelRatio": 3.0 },
  85. "userAgent": "Mozilla/5.0 (Linux; Android 4.2.1; en-us; Nexus 5 Build/JOP40D) AppleWebKit/535.19 (KHTML, like Gecko) Chrome/18.0.1025.166 Mobile Safari/535.19" }
  86. try:
  87. driver = webdriver.Chrome(options=options)
  88. # driver = webdriver.Chrome(ChromeDriverManager().install(),options=options)
  89. ## Create Undetected Chromedriver with Options
  90. # driver = uc.Chrome(options=options)
  91. # options = uc.ChromeOptions()
  92. except:
  93. traceback.print_exc()
  94. #kw='真理大學國際生'
  95. #domain='cia.au.edu.tw'
  96. if custname is None:
  97. custname='真理'
  98. cursor=db.query("SELECT cust,plan,prefix,domain,kw,positive FROM public.seo_jobs where cust='"+custname+"' order by random() limit 1")
  99. kw=None
  100. domain=None
  101. for c in cursor:
  102. cust=c['cust']
  103. kw=c['kw']
  104. plan=c['plan']
  105. prefix=c['prefix']
  106. domain=eval(c['domain'])[0]
  107. positive=eval(c['positive'])
  108. break
  109. #kw='真理大學教堂'
  110. #domain='udn.com'
  111. print(kw)
  112. print(domain)
  113. #kw='真理大學校友'
  114. #kw='真理大學國際生'
  115. #kw='真理大學張聰聯'
  116. #domain='au.edu.tw'
  117. #domain='pronews.tw'
  118. driver.get('https://www.google.com?num=100')
  119. time.sleep(3)
  120. print(driver.current_url)
  121. elmt = driver.find_element(By.XPATH, "//textarea[@name='q']")
  122. time.sleep(1)
  123. elmt.send_keys(kw)
  124. elmt.send_keys(Keys.ENTER)
  125. time.sleep(4)
  126. elmts = driver.find_elements(By.XPATH, "//a[@jsname='UWckNb']")
  127. numresults=len(elmts)
  128. print('搜尋結果數量',numresults)
  129. if numresults<=0:
  130. print(elmts)
  131. for elmt in elmts:
  132. href=elmt.get_attribute('href')
  133. txt=elmt.text
  134. # print(txt)
  135. # print(href)
  136. if domain in href:
  137. webdriver.ActionChains(driver).move_to_element(elmt).perform()
  138. webdriver.ActionChains(driver).move_to_element(elmt).click().perform()
  139. print(domain)
  140. print(href)
  141. driver.quit()
  142. return txt
  143. break
  144. return '{empty}'
  145. # time.sleep(5)