cel_seo_click.py 3.0 KB

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