general_clickbot_local.py 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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.support import expected_conditions as EC
  12. import codecs
  13. import random
  14. import datetime
  15. import dataset
  16. import pymysql
  17. pymysql.install_as_MySQLdb()
  18. import time
  19. import traceback
  20. import sys
  21. import fire
  22. driver = None
  23. def empty_query(q):
  24. global driver
  25. googleurl = 'https://www.google.com/search?q=' + urllib.parse.quote(q)
  26. driver.get(googleurl)
  27. time.sleep(3)
  28. def process_query(qs):
  29. # db = dataset.connect('mysql://choozmo:pAssw0rd@db.ptt.cx:3306/seo?charset=utf8mb4')
  30. db = dataset.connect('postgresql://postgres:eyJhbGciOiJI@172.105.241.163:5432/postgres')
  31. table = db['nad_log']
  32. q = qs[0]
  33. domain = qs[1]
  34. client=qs[2]
  35. global driver
  36. googleurl = 'https://www.google.com/search?q={}&num={}&hl={}'.format(urllib.parse.quote(q), 100, 'zh-TW')
  37. print(googleurl)
  38. driver.get(googleurl)
  39. time.sleep(6)
  40. elmts = driver.find_elements(By.XPATH, "//div[@class='yuRUbf']/a")
  41. idx = 1
  42. ranking = -1
  43. result = len(elmts)
  44. # driver.save_screenshot('c:/tmp/test.png')
  45. for elmt in elmts:
  46. href = elmt.get_attribute('href')
  47. txt = elmt.text
  48. # print(txt)
  49. if domain in href:
  50. print(href)
  51. print(txt)
  52. print("ranking", idx)
  53. table.insert({'ranking':idx,'kw':q,'results':result,'url':href,'title':txt,'dt':datetime.datetime.now(),'client':client})
  54. db.close()
  55. print('clicked....')
  56. webdriver.ActionChains(driver).move_to_element(elmt).perform()
  57. webdriver.ActionChains(driver).move_to_element(elmt).click().perform()
  58. time.sleep(5)
  59. els
  60. idx += 1
  61. def run_once(q):
  62. global driver
  63. result = []
  64. user_agent = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.88 Safari/537.36'
  65. s = Service('C:\/Users\/s1301\/Downloads\/chromedriver_110\/chromedriver')
  66. options = webdriver.ChromeOptions()
  67. options.add_argument('--headless')
  68. # options.add_argument('--remote-debugging-port=9222')
  69. # options.add_experimental_option("debuggerAddress", "192.168.192.45:9922")
  70. options.add_argument("--user-agent=" +user_agent)
  71. options.add_argument("--incognito")
  72. # options.add_argument('--proxy-server=socks5://172.104.93.163:41800')
  73. driver = webdriver.Chrome(
  74. options=options, service=s)
  75. str1 = driver.capabilities['browserVersion']
  76. print('版本', str1)
  77. driver.delete_all_cookies()
  78. driver.set_window_size(1400, 1000)
  79. print(q)
  80. process_query(q)
  81. time.sleep(3)
  82. driver.quit()
  83. # for c in lst:ㄕ
  84. # while True:
  85. # try:
  86. # c=random.choice(lst)
  87. # except:
  88. # traceback.print_exc()
  89. # sleepint=random.randint(320,520)
  90. # time.sleep(sleepint)
  91. class JParams(object):
  92. def get(self, kw, domain,client):
  93. print(kw)
  94. print(domain)
  95. run_once((kw, domain,client))
  96. if __name__ == '__main__':
  97. fire.Fire(JParams)