general_clickbot_hhh.py 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. import time
  2. from selenium import webdriver
  3. from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
  4. import time
  5. import os
  6. import urllib.parse
  7. from selenium.webdriver.support.ui import WebDriverWait
  8. from selenium.webdriver.common.by import By
  9. from selenium.webdriver.chrome.service import Service
  10. from selenium.webdriver.support import expected_conditions as EC
  11. import codecs
  12. import random
  13. import datetime
  14. import dataset
  15. import time
  16. import traceback
  17. import sys
  18. import random
  19. import requests
  20. db = dataset.connect('mysql://choozmo:pAssw0rd@db.ptt.cx:3306/seo?charset=utf8mb4')
  21. table=db['general_log']
  22. driver = None
  23. headers = {
  24. "Authorization": "Bearer " + "6SDULL1Ebklduc6TFxa97AFto5Sj21kyJ30CxiLiSoi",
  25. "Content-Type": "application/x-www-form-urlencoded"
  26. }
  27. def rua():
  28. pool = [
  29. "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:73.0) Gecko/20100101 Firefox/73.0",
  30. "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:76.0) Gecko/20100101 Firefox/76.0",
  31. "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.88 Safari/537.36"
  32. "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.101 Safari/537.36",
  33. "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.108 Safari/537.36",
  34. "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.97 Safari/537.36",
  35. "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.138 Safari/537.36 OPR/68.0.3618.125",
  36. ]
  37. return random.choice(pool)
  38. def send_msg(kw):
  39. params = {"message": "msg: "+kw}
  40. r = requests.post("https://notify-api.line.me/api/notify",headers=headers, params=params)
  41. def empty_query(q):
  42. global driver
  43. googleurl='https://www.google.com/search?q='+urllib.parse.quote(q)
  44. driver.get(googleurl)
  45. time.sleep(3)
  46. def process_query(qs):
  47. q=qs[0]
  48. domain=qs[1]
  49. global driver
  50. googleurl = 'https://www.google.com/search?q={}&num={}&hl={}'.format(urllib.parse.quote(q), 100,'zh-TW')
  51. print(googleurl)
  52. driver.get(googleurl)
  53. time.sleep(6)
  54. while True:
  55. try:
  56. elmts = driver.find_elements(By.XPATH, "//div[@class='yuRUbf']/a")
  57. print('尋找')
  58. break
  59. except:
  60. pass
  61. idx=1
  62. ranking=-1
  63. print('搜尋結果數量',len(elmts))
  64. if len(elmts) <=0:
  65. send_msg('network failed...')
  66. # driver.save_screenshot('c:/tmp/test.png')
  67. for elmt in elmts:
  68. href=elmt.get_attribute('href')
  69. txt=elmt.text
  70. if len(txt)>10:
  71. if domain in href:
  72. print('clicked....')
  73. print(href)
  74. print(txt)
  75. print("ranking", idx)
  76. table.insert({'kw':q,'domain':domain,'ranking':idx,'title':txt,'url':href,'dt':datetime.datetime.now(),'num':1})
  77. webdriver.ActionChains(driver).move_to_element(elmt).perform()
  78. webdriver.ActionChains(driver).move_to_element(elmt).click().perform()
  79. break
  80. idx+=1
  81. def run_once(q):
  82. global driver
  83. result=[]
  84. # s = Service('/root/driver/chromedriver')
  85. # s = Service('/Users/zooeytsai/Downloads/chromedriver 2')
  86. user_agent = rua()
  87. options = webdriver.ChromeOptions()
  88. options.add_argument('--headless')
  89. options.add_argument("--no-sandbox")
  90. options.add_argument("--disable-dev-shm-usage")
  91. # options.add_argument("start-maximized")
  92. # options.add_argument('--remote-debugging-port='+str(q[2]))
  93. # options.add_argument('--remote-debugging-port=9222')
  94. # options.add_argument("--user-agent=" +user_agent)
  95. options.add_argument("--incognito")
  96. # driver = webdriver.Chrome(options=options,service=s)
  97. print('before init')
  98. driver = webdriver.Chrome(options=options)
  99. print('after init')
  100. driver.delete_all_cookies()
  101. # driver.set_window_size(1400,1000)
  102. print('到此')
  103. process_query(q)
  104. time.sleep(3)
  105. driver.quit()
  106. sys.exit()
  107. cursor = db.query('SELECT query FROM seo.hhh_gsc_imp where position >=2.5 and position <=8.5 order by rand() limit 1')
  108. query=None
  109. for c in cursor:
  110. query=c['query']
  111. print(query)
  112. break
  113. run_once((query,'hhh.com.tw'))