sns_clickbot.py 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. import time
  2. import json
  3. from selenium import webdriver
  4. from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
  5. import time
  6. import os
  7. import urllib.parse
  8. from selenium.webdriver.support.ui import WebDriverWait
  9. from selenium.webdriver.common.by import By
  10. from selenium.webdriver.chrome.service import Service
  11. from selenium.webdriver.common.keys import Keys
  12. from selenium.webdriver.support import expected_conditions as EC
  13. import codecs
  14. import random
  15. import requests
  16. import datetime
  17. import dataset
  18. import time
  19. import traceback
  20. import sys
  21. import fire
  22. #import pymysql
  23. #pymysql.install_as_MySQLdb()
  24. driver = None
  25. def rua():
  26. pool = [
  27. "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:73.0) Gecko/20100101 Firefox/73.0",
  28. "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:76.0) Gecko/20100101 Firefox/76.0",
  29. "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.88 Safari/537.36"
  30. "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.101 Safari/537.36",
  31. "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.108 Safari/537.36",
  32. "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.97 Safari/537.36",
  33. "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",
  34. ]
  35. return random.choice(pool)
  36. def empty_query(q):
  37. global driver
  38. googleurl='https://www.google.com/search?q='+urllib.parse.quote(q)
  39. driver.get(googleurl)
  40. time.sleep(3)
  41. def process_query(qs):
  42. db = dataset.connect('mysql://choozmo:pAssw0rd@db.ptt.cx:3306/seo?charset=utf8mb4')
  43. table=db['sns_log']
  44. q=qs[0]
  45. url=qs[1]
  46. client=qs[2]
  47. domain=qs[3]
  48. global driver
  49. googleurl = 'https://www.google.com/?num=100'
  50. driver.get(googleurl)
  51. time.sleep(6)
  52. send_kw_elmt = driver.find_element(By.XPATH, '/html/body/div[1]/div[3]/form/div[1]/div[1]/div[1]/div/div[2]/input')
  53. send_kw_elmt.send_keys(q)
  54. time.sleep(3)
  55. send_kw_elmt.send_keys(Keys.ENTER)
  56. time.sleep(6)
  57. time.sleep(10)
  58. elmts = driver.find_elements(By.XPATH,"//div[@class='yuRUbf']/a")
  59. print('網頁數量',len(elmts))
  60. idx = 1
  61. for elmt in elmts:
  62. href=elmt.get_attribute('href')
  63. txt=elmt.text
  64. if len(txt)>10:
  65. if href == url:
  66. print('clicked....')
  67. print(href)
  68. print(txt)
  69. print("ranking", idx)
  70. table.insert({'kw':q,'client':client,'ranking':idx,'title':txt,'url':href,'dt':datetime.datetime.now(),'domain':domain})
  71. webdriver.ActionChains(driver).move_to_element(elmt).perform()
  72. webdriver.ActionChains(driver).move_to_element(elmt).click().perform()
  73. time.sleep(5)
  74. break
  75. idx+=1
  76. db.close()
  77. def run_once(q):
  78. global driver
  79. s = Service('/root/driver/chromedriver')
  80. user_agent = rua()
  81. options = webdriver.ChromeOptions()
  82. options.add_argument('--headless')
  83. options.add_argument('--remote-debugging-port=9222')
  84. options.add_experimental_option("debuggerAddress", f"127.0.0.1:{q[3]}")
  85. options.add_argument("--user-agent=" +user_agent)
  86. options.add_argument("--incognito")
  87. driver = webdriver.Chrome(
  88. options=options,service=s)
  89. driver.delete_all_cookies()
  90. driver.set_window_size(1400,1000)
  91. process_query(q)
  92. time.sleep(3)
  93. driver.quit()
  94. class JParams(object):
  95. def get(self, kw,url,client,port,domain):
  96. print('關鍵字',kw)
  97. run_once( (kw,url,client,port,domain) )
  98. if __name__ == '__main__':
  99. fire.Fire(JParams)