general_clickbot.py 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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['general_log']
  44. q=qs[0]
  45. domain=qs[1]
  46. global driver
  47. googleurl = 'https://www.google.com/?num=100'
  48. driver.get(googleurl)
  49. time.sleep(6)
  50. 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')
  51. send_kw_elmt.send_keys(q)
  52. time.sleep(3)
  53. send_kw_elmt.send_keys(Keys.ENTER)
  54. time.sleep(6)
  55. idx=1
  56. ranking=-1
  57. print('網頁數量',len(elmts))
  58. # driver.save_screenshot('c:/tmp/test.png')
  59. if 'site' in q:
  60. href = elmts[0].get_attribute('href')
  61. txt = elmts[0].text
  62. print('clicked....')
  63. print(href)
  64. print(txt)
  65. print("ranking", idx)
  66. table.insert(
  67. {'kw': q, 'domain': domain, 'ranking': idx, 'title': txt, 'url': href, 'dt': datetime.datetime.now()})
  68. webdriver.ActionChains(driver).move_to_element(elmts[0]).perform()
  69. webdriver.ActionChains(driver).move_to_element(elmts[0]).click().perform()
  70. time.sleep(5)
  71. for elmt in elmts:
  72. href=elmt.get_attribute('href')
  73. txt=elmt.text
  74. if domain in href:
  75. print('clicked....')
  76. print('點擊網址',href)
  77. print('標題',txt)
  78. print("ranking", idx)
  79. table.insert({'kw':q,'domain':domain,'ranking':idx,'title':txt,'url':href,'dt':datetime.datetime.now()})
  80. webdriver.ActionChains(driver).move_to_element(elmt).perform()
  81. webdriver.ActionChains(driver).move_to_element(elmt).click().perform()
  82. time.sleep(5)
  83. break
  84. idx+=1
  85. db.close()
  86. driver.quit()
  87. def run_once(q):
  88. global driver
  89. result=[]
  90. s = Service('/root/driver/chromedriver')
  91. user_agent = rua()
  92. options = webdriver.ChromeOptions()
  93. options.add_argument('--headless')
  94. options.add_argument('--remote-debugging-port=9222')
  95. options.add_experimental_option("debuggerAddress", f"127.0.0.1:{q[2]}")
  96. options.add_argument("--user-agent=" +user_agent)
  97. options.add_argument("--incognito")
  98. driver = webdriver.Chrome(
  99. options=options,service=s)
  100. driver.delete_all_cookies()
  101. driver.set_window_size(1400,1000)
  102. process_query(q)
  103. time.sleep(3)
  104. #for c in lst:
  105. #while True:
  106. # try:
  107. # c=random.choice(lst)
  108. # except:
  109. # traceback.print_exc()
  110. # sleepint=random.randint(320,520)
  111. # time.sleep(sleepint)
  112. class JParams(object):
  113. def get(self, kw,domain,port):
  114. run_once( (kw,domain,port) )
  115. if __name__ == '__main__':
  116. fire.Fire(JParams)