cel_seo_click.py 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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. import json
  12. from celery.schedules import crontab
  13. app = Celery('tasks', backend ='redis://172.104.92.245', broker='redis://172.104.92.245')
  14. @app.on_after_configure.connect
  15. def setup_periodic_tasks(sender, **kwargs):
  16. # print(app.request.delivery_info['routing_key'])
  17. json_str = json.dumps(sender, indent=4)
  18. print(json_str)
  19. #sender task.request.hostnam
  20. sender.add_periodic_task(100.0, selenium_jared_click.s(), name='add every 2 min')
  21. @app.task
  22. def selenium_jared_click():
  23. db = dataset.connect('postgresql://postgres:eyJhbGciOiJI@172.105.241.163:5432/postgres')
  24. options = webdriver.ChromeOptions()
  25. options.add_argument("--no-sandbox")
  26. options.add_argument("--headless")
  27. options.add_argument("--incognito")
  28. #options.add_argument('--blink-settings=imagesEnabled=false')
  29. mobile_emulation = {
  30. "deviceMetrics": { "width": 360, "height": 640, "pixelRatio": 3.0 },
  31. "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" }
  32. try:
  33. driver = webdriver.Chrome(options=options)
  34. # driver = webdriver.Chrome(ChromeDriverManager().install(),options=options)
  35. ## Create Undetected Chromedriver with Options
  36. # driver = uc.Chrome(options=options)
  37. # options = uc.ChromeOptions()
  38. except:
  39. traceback.print_exc()
  40. #kw='真理大學國際生'
  41. #domain='cia.au.edu.tw'
  42. cursor=db.query("SELECT cust,plan,prefix,domain,kw,positive FROM public.seo_jobs where cust='真理' order by random() limit 1")
  43. kw=None
  44. domain=None
  45. for c in cursor:
  46. cust=c['cust']
  47. kw=c['kw']
  48. plan=c['plan']
  49. prefix=c['prefix']
  50. domain=eval(c['domain'])[0]
  51. positive=eval(c['positive'])
  52. break
  53. #kw='真理大學教堂'
  54. #domain='udn.com'
  55. print(kw)
  56. print(domain)
  57. #kw='真理大學校友'
  58. #kw='真理大學國際生'
  59. #kw='真理大學張聰聯'
  60. #domain='au.edu.tw'
  61. #domain='pronews.tw'
  62. driver.get('https://www.google.com?num=100')
  63. time.sleep(3)
  64. print(driver.current_url)
  65. elmt = driver.find_element(By.XPATH, "//textarea[@name='q']")
  66. time.sleep(1)
  67. elmt.send_keys(kw)
  68. elmt.send_keys(Keys.ENTER)
  69. time.sleep(4)
  70. elmts = driver.find_elements(By.XPATH, "//a[@jsname='UWckNb']")
  71. numresults=len(elmts)
  72. print('搜尋結果數量',numresults)
  73. if numresults<=0:
  74. print(elmts)
  75. for elmt in elmts:
  76. href=elmt.get_attribute('href')
  77. txt=elmt.text
  78. # print(txt)
  79. # print(href)
  80. if domain in href:
  81. webdriver.ActionChains(driver).move_to_element(elmt).perform()
  82. webdriver.ActionChains(driver).move_to_element(elmt).click().perform()
  83. print(domain)
  84. print(href)
  85. driver.quit()
  86. return txt
  87. break
  88. return '{empty}'
  89. # time.sleep(5)