general_clickbot_local.py 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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 requests
  15. import datetime
  16. import dataset
  17. import pymysql
  18. pymysql.install_as_MySQLdb()
  19. import time
  20. import traceback
  21. import sys
  22. import fire
  23. driver = None
  24. def empty_query(q):
  25. global driver
  26. googleurl = 'https://www.google.com/search?q=' + urllib.parse.quote(q)
  27. driver.get(googleurl)
  28. time.sleep(3)
  29. def process_query(qs):
  30. db = dataset.connect('mysql://choozmo:pAssw0rd@db.ptt.cx:3306/seo?charset=utf8mb4')
  31. table = db['general_log']
  32. q = qs[0]
  33. domain = qs[1]
  34. client='引新聞'
  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. print(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({'kw':q,'client':client,'ranking':idx,'title':txt,'url':href,'dt':datetime.datetime.now(),'domain':domain})
  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. break
  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('/Users/zooeytsai/Downloads/chromedriver 4')
  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):
  93. print(kw)
  94. print(domain)
  95. run_once((kw, domain))
  96. if __name__ == '__main__':
  97. fire.Fire(JParams)