general_clickbot.py 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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.support import expected_conditions as EC
  11. import codecs
  12. import random
  13. import requests
  14. import datetime
  15. import dataset
  16. import time
  17. import traceback
  18. import sys
  19. import fire
  20. db = dataset.connect('mysql://choozmo:pAssw0rd@db.ptt.cx:3306/seo?charset=utf8mb4')
  21. table=db['general_log']
  22. driver=None
  23. headers = {
  24. "Authorization": "Bearer " + "t35vhZtWNgvDNWHc3DJh0OKll3mcB9GvC8K2EAkBug2",
  25. "Content-Type": "application/x-www-form-urlencoded"
  26. }
  27. def send_msg(kw):
  28. params = {"message": "處理關鍵字: "+kw}
  29. r = requests.post("https://notify-api.line.me/api/notify",headers=headers, params=params)
  30. def empty_query(q):
  31. global driver
  32. googleurl='https://www.google.com/search?q='+urllib.parse.quote(q)
  33. driver.get(googleurl)
  34. time.sleep(3)
  35. def process_query(qs):
  36. q=qs[0]
  37. domain=qs[1]
  38. global driver
  39. googleurl = 'https://www.google.com/search?q={}&num={}&hl={}'.format(urllib.parse.quote(q), 100,'zh-TW')
  40. print(googleurl)
  41. driver.get(googleurl)
  42. time.sleep(6)
  43. elmts=driver.find_elements_by_xpath("//div[@class='yuRUbf']/a")
  44. idx=1
  45. ranking=-1
  46. print(len(elmts))
  47. # driver.save_screenshot('c:/tmp/test.png')
  48. for elmt in elmts:
  49. href=elmt.get_attribute('href')
  50. txt=elmt.text
  51. if len(txt)>10:
  52. if domain in href:
  53. print('clicked....')
  54. print(href)
  55. print(txt)
  56. print("ranking", idx)
  57. table.insert({'kw':q,'domain':domain,'ranking':idx,'title':txt,'url':href,'dt':datetime.datetime.now()})
  58. webdriver.ActionChains(driver).move_to_element(elmt).perform()
  59. webdriver.ActionChains(driver).move_to_element(elmt).click().perform()
  60. break
  61. idx+=1
  62. def run_once(q):
  63. global driver
  64. result=[]
  65. options = webdriver.ChromeOptions()
  66. options.add_argument('--headless')
  67. # options.add_argument("--user-agent=" +user_agent)
  68. options.add_argument("--incognito")
  69. driver = webdriver.Chrome(
  70. options=options)
  71. driver.delete_all_cookies()
  72. driver.set_window_size(1400,1000)
  73. print(q)
  74. process_query(q)
  75. time.sleep(3)
  76. driver.quit()
  77. #for c in lst:
  78. #while True:
  79. # try:
  80. # c=random.choice(lst)
  81. # except:
  82. # traceback.print_exc()
  83. # sleepint=random.randint(320,520)
  84. # time.sleep(sleepint)
  85. class JParams(object):
  86. def get(self, kw,domain):
  87. print(kw)
  88. print(domain)
  89. run_once( (kw,domain) )
  90. if __name__ == '__main__':
  91. fire.Fire(JParams)