bennisclickjob.py 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. import time
  2. from datetime import datetime
  3. import json
  4. from selenium import webdriver
  5. from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
  6. import time
  7. import os
  8. import urllib.parse
  9. from selenium.webdriver.support.ui import WebDriverWait
  10. from selenium.webdriver.common.by import By
  11. from selenium.webdriver.support import expected_conditions as EC
  12. import codecs
  13. import random
  14. import requests
  15. import dataset
  16. import traceback
  17. import sys
  18. from selenium.webdriver.common.keys import Keys
  19. target_domain=['bennis.com.tw']
  20. brands={'bennis.com.tw':'班尼斯'}
  21. driver=None
  22. headers = {
  23. "Authorization": "Bearer " + "t35vhZtWNgvDNWHc3DJh0OKll3mcB9GvC8K2EAkBug2",
  24. "Content-Type": "application/x-www-form-urlencoded"
  25. }
  26. def send_msg(kw):
  27. params = {"message": "處理關鍵字: "+kw}
  28. r = requests.post("https://notify-api.line.me/api/notify",headers=headers, params=params)
  29. def empty_query(q):
  30. global driver
  31. googleurl='https://www.google.com/search?q='+urllib.parse.quote(q)
  32. driver.get(googleurl)
  33. time.sleep(3)
  34. def process_query():
  35. q="班尼斯"
  36. domain="bennis.com.tw"
  37. global driver
  38. driver.get('https://www.google.com?num=100')
  39. time.sleep(3)
  40. print(driver.current_url)
  41. # elmts=driver.find_elements_by_xpath("//div[@class='yuRUbf']/a")
  42. # ABOVE METHOD IS DEPRECATED STARTING SELENIUM 4.3.0, USE THIS
  43. #
  44. elmt = driver.find_element(By.XPATH, "//input[@name='q']")
  45. time.sleep(1)
  46. elmt.send_keys(q)
  47. elmt.send_keys(Keys.ENTER)
  48. idx=1
  49. ranking=-1
  50. domain_in_link = 0
  51. googleurl = driver.current_url
  52. print(driver.current_url)
  53. elmts=driver.find_elements("xpath","//div[@class='yuRUbf']/a")
  54. print (len(elmts))
  55. # driver.save_screenshot('c:/tmp/test.png')
  56. for el in elmts:
  57. href=el.get_attribute('href')
  58. txt=el.text
  59. if len(txt)>10:
  60. if domain in href:
  61. domain_in_link += 1
  62. print('clicked....')
  63. print(href)
  64. print(txt)
  65. webdriver.ActionChains(driver).move_to_element(el).perform()
  66. webdriver.ActionChains(driver).move_to_element(el).click().perform()
  67. time.sleep(6)
  68. if domain in target_domain:
  69. print("Target link found")
  70. time_stamp = datetime.fromtimestamp(time.time())
  71. time_stamp = time_stamp.strftime("%Y-%m-%d %H:%M:%S")
  72. db['click_results'].insert({"time_stamp": time_stamp, "brand": brands[domain], "domain": domain, "query": q, "url": href, "content": txt})
  73. break
  74. if domain in target_domain:
  75. print("Target domain found")
  76. time_stamp = datetime.fromtimestamp(time.time())
  77. time_stamp = time_stamp.strftime("%Y-%m-%d %H:%M:%S")
  78. db['query_results'].insert({"time_stamp": time_stamp, "brand": brands[domain], "domain": domain, "query": q, "googleurl": googleurl, "element_count": len(elmts), "domain_in_link_count": domain_in_link})
  79. print(domain_in_link)
  80. def run_once():
  81. global driver
  82. result=[]
  83. options = webdriver.ChromeOptions()
  84. options.add_argument('--headless')
  85. # options.add_argument("--user-agent=" +user_agent)
  86. options.add_argument("--incognito")
  87. options.add_argument('--no-sandbox')
  88. options.add_argument('--disable-dev-shm-usage')
  89. driver = webdriver.Chrome(
  90. options=options)
  91. driver.delete_all_cookies()
  92. driver.set_window_size(1400,1000)
  93. process_query()
  94. time.sleep(3)
  95. driver.quit()
  96. #execution starts here
  97. db = dataset.connect('mysql://choozmo:pAssw0rd@db.ptt.cx:3306/seo?charset=utf8mb4')
  98. while True:
  99. try:
  100. run_once()
  101. except:
  102. traceback.print_exc()
  103. sleepint=random.randint(35,50)
  104. print("Completed (" + str(sleepint) + ")")
  105. time.sleep(sleepint)