docker_chrome_test.py 3.0 KB

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