general_clickjob.py 2.6 KB

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