general_clickbot.py 2.8 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 time
  18. import traceback
  19. import sys
  20. import fire
  21. import pymysql
  22. pymysql.install_as_MySQLdb()
  23. from ig_tags import setting
  24. db = dataset.connect('mysql://choozmo:pAssw0rd@db.ptt.cx:3306/seo?charset=utf8mb4')
  25. table=db['general_log']
  26. driver = None
  27. def empty_query(q):
  28. global driver
  29. googleurl='https://www.google.com/search?q='+urllib.parse.quote(q)
  30. driver.get(googleurl)
  31. time.sleep(3)
  32. def process_query(qs):
  33. q=qs[0]
  34. domain=qs[1]
  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.T,"//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. if len(txt)>10:
  49. if domain in href:
  50. print('clicked....')
  51. print(href)
  52. print(txt)
  53. print("ranking", idx)
  54. table.insert({'kw':q,'domain':domain,'ranking':idx,'title':txt,'url':href,'dt':datetime.datetime.now()})
  55. webdriver.ActionChains(driver).move_to_element(elmt).perform()
  56. webdriver.ActionChains(driver).move_to_element(elmt).click().perform()
  57. break
  58. idx+=1
  59. def run_once(q):
  60. global driver
  61. result=[]
  62. s = Service('/root/driver/chromedriver')
  63. user_agent = setting.rua()
  64. options = webdriver.ChromeOptions()
  65. options.add_argument('--headless')
  66. options.add_argument('--remote-debugging-port=9222')
  67. options.add_experimental_option("debuggerAddress", "127.0.0.1:9922")
  68. options.add_argument("--user-agent=" +user_agent)
  69. options.add_argument("--incognito")
  70. driver = webdriver.Chrome(
  71. options=options,service=s)
  72. driver.delete_all_cookies()
  73. driver.set_window_size(1400,1000)
  74. print('到此')
  75. process_query(q)
  76. time.sleep(3)
  77. driver.quit()
  78. #for c in lst:
  79. #while True:
  80. # try:
  81. # c=random.choice(lst)
  82. # except:
  83. # traceback.print_exc()
  84. # sleepint=random.randint(320,520)
  85. # time.sleep(sleepint)
  86. class JParams(object):
  87. def get(self, kw,domain):
  88. print('kw')
  89. print(domain)
  90. run_once( (kw,domain) )
  91. if __name__ == '__main__':
  92. fire.Fire(JParams)