general_clickbot_3.py 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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 redis
  23. driver = None
  24. def process_query(qs):
  25. db = dataset.connect('mysql://choozmo:pAssw0rd@db.ptt.cx:3306/seo?charset=utf8mb4')
  26. table = db['general_log']
  27. q = qs[0]
  28. domain = qs[1]
  29. global driver
  30. googleurl = 'https://www.google.com/?num=100'
  31. driver.get(googleurl)
  32. time.sleep(6)
  33. send_kw_elmt = WebDriverWait(driver, 10).until(EC.presence_of_element_located(
  34. (By.XPATH, '/html/body/div[1]/div[3]/form/div[1]/div[1]/div[1]/div/div[2]/input')))
  35. send_kw_elmt.send_keys(q)
  36. time.sleep(3)
  37. send_kw_elmt.send_keys(Keys.ENTER)
  38. time.sleep(6)
  39. print(driver.current_url)
  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. if 'site' in q:
  46. href = elmts[0].get_attribute('href')
  47. txt = elmts[0].text
  48. print('clicked....')
  49. print(href)
  50. print(txt)
  51. print("ranking", idx)
  52. table.insert(
  53. {'kw': q, 'domain': domain, 'ranking': idx, 'title': txt, 'url': href, 'dt': datetime.datetime.now()})
  54. webdriver.ActionChains(driver).move_to_element(elmts[0]).perform()
  55. time.sleep(3)
  56. webdriver.ActionChains(driver).move_to_element(elmts[0]).click().perform()
  57. time.sleep(5)
  58. else:
  59. for elmt in elmts:
  60. href = elmt.get_attribute('href')
  61. txt = elmt.text
  62. if len(txt) > 10:
  63. if domain in href:
  64. print('clicked....')
  65. print('點擊網址', href)
  66. print('標題', txt)
  67. print("ranking", idx)
  68. webdriver.ActionChains(driver).move_to_element(elmt).perform()
  69. time.sleep(3)
  70. webdriver.ActionChains(driver).move_to_element(elmt).click().perform()
  71. table.insert({'kw': q, 'domain': domain, 'ranking': idx, 'title': txt, 'url': href,'dt': datetime.datetime.now()})
  72. time.sleep(5)
  73. break
  74. idx += 1
  75. db.close()
  76. print('資料庫關閉')
  77. driver.quit()
  78. def run_once(q):
  79. global driver
  80. s = Service('/root/driver/chromedriver')
  81. options = webdriver.ChromeOptions()
  82. options.add_argument("--no-sandbox")
  83. options.add_argument("--disable-dev-shm-usage")
  84. options.add_argument('--headless')
  85. # options.add_argument('--remote-debugging-port=9222')
  86. # options.add_experimental_option("debuggerAddress", f"127.0.0.1:{q[2]}")
  87. # options.add_argument("--user-agent=" +user_agent)
  88. options.add_argument("--incognito")
  89. r = redis.Redis(host='db.ptt.cx', port=6379, db=2, password='choozmo9')
  90. data = r.get('google_proxy')
  91. jstext = data.decode('utf-8')
  92. jsobj = json.loads(jstext)
  93. # print('Free proxy',jsobj)
  94. proxy = random.choice(jsobj)
  95. i5 = "--proxy-server=socks5://172.104.93.163:41800"
  96. change_ip_list = ['--proxy-server=%s' % proxy, "--proxy-server=socks5://127.0.0.1:9050",
  97. "--proxy-server=socks5://192.53.174.202:8180"]
  98. change_ip = random.choice(change_ip_list)
  99. options.add_argument(i5)
  100. # print('使用代理ip',change_ip)
  101. driver = webdriver.Chrome(options=options, service=s)
  102. driver.delete_all_cookies()
  103. driver.set_window_size(1400, 1000)
  104. process_query(q)
  105. # for c in lst:
  106. # while True:
  107. # try:
  108. # c=random.choice(lst)
  109. # except:
  110. # traceback.print_exc()
  111. # sleepint=random.randint(320,520)
  112. # time.sleep(sleepint)
  113. class JParams(object):
  114. def get(self, kw, domain):
  115. run_once((kw, domain))
  116. if __name__ == '__main__':
  117. fire.Fire(JParams)