cel_seo_click.py 2.8 KB

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