general_click_tor.py 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. import time
  2. from selenium import webdriver
  3. from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
  4. import time
  5. import os
  6. import urllib.parse
  7. from selenium.webdriver.support.ui import WebDriverWait
  8. from selenium.webdriver.common.by import By
  9. from selenium.webdriver.chrome.service import Service
  10. from selenium.webdriver.support import expected_conditions as EC
  11. from selenium.webdriver.common.keys import Keys
  12. import codecs
  13. import random
  14. import datetime
  15. import dataset
  16. import time
  17. import traceback
  18. import sys
  19. import random
  20. import socket
  21. import requests
  22. db = dataset.connect('mysql://choozmo:pAssw0rd@db.ptt.cx:3306/seo?charset=utf8mb4')
  23. table=db['general_log']
  24. driver = None
  25. headers = {
  26. "Authorization": "Bearer " + "6SDULL1Ebklduc6TFxa97AFto5Sj21kyJ30CxiLiSoi",
  27. "Content-Type": "application/x-www-form-urlencoded"
  28. }
  29. def rua():
  30. pool = [
  31. "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:73.0) Gecko/20100101 Firefox/73.0",
  32. "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:76.0) Gecko/20100101 Firefox/76.0",
  33. "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.88 Safari/537.36"
  34. "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.101 Safari/537.36",
  35. "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.108 Safari/537.36",
  36. "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.97 Safari/537.36",
  37. "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.138 Safari/537.36 OPR/68.0.3618.125",
  38. ]
  39. return random.choice(pool)
  40. def send_msg(kw):
  41. hname=socket.gethostname()
  42. params = {"message": hname+": "+kw}
  43. r = requests.post("https://notify-api.line.me/api/notify",headers=headers, params=params)
  44. def empty_query(q):
  45. global driver
  46. googleurl='https://www.google.com/search?q='+urllib.parse.quote(q)
  47. driver.get(googleurl)
  48. time.sleep(3)
  49. def process_query(qs):
  50. q=qs[0]
  51. domain=qs[1]
  52. global driver
  53. # googleurl = 'https://www.google.com/search?q={}&num={}&hl={}'.format(urllib.parse.quote(q), 100,'zh-TW')
  54. googleurl = 'https://www.google.com/'
  55. print(googleurl)
  56. driver.get(googleurl)
  57. time.sleep(6)
  58. # if 'sorry' in driver.page_url:
  59. # return None
  60. try:
  61. elmt = driver.find_element(By.XPATH, "//button[@id='L2AGLb']")
  62. if elmt:
  63. elmt.click()
  64. except:
  65. print('exception')
  66. try:
  67. elmt = driver.find_element(By.XPATH, "//input[@aria-label='搜尋']")
  68. if elmt:
  69. elmt.send_keys(q)
  70. elmt.send_keys(Keys.ENTER)
  71. except:
  72. print(elmt)
  73. while True:
  74. try:
  75. elmts = driver.find_elements(By.XPATH, "//div[@class='yuRUbf']/a")
  76. print('尋找')
  77. time.sleep(2)
  78. break
  79. except:
  80. pass
  81. idx=1
  82. ranking=-1
  83. print('搜尋結果數量',len(elmts))
  84. if len(elmts) <=0:
  85. send_msg('network failed...')
  86. # driver.save_screenshot('c:/tmp/test.png')
  87. for elmt in elmts:
  88. href=elmt.get_attribute('href')
  89. txt=elmt.text
  90. if len(txt)>10:
  91. if domain in href:
  92. print('clicked....')
  93. print(href)
  94. print(txt)
  95. print("ranking", idx)
  96. table.insert({'kw':q,'domain':domain,'ranking':idx,'title':txt,'url':href,'dt':datetime.datetime.now(),'num':1})
  97. webdriver.ActionChains(driver).move_to_element(elmt).perform()
  98. webdriver.ActionChains(driver).move_to_element(elmt).click().perform()
  99. break
  100. idx+=1
  101. return "ok"
  102. def run_once(q):
  103. global driver
  104. result=[]
  105. os.system('docker container restart proxy1')
  106. time.sleep(12)
  107. # s = Service('/root/driver/chromedriver')
  108. # s = Service('/Users/zooeytsai/Downloads/chromedriver 2')
  109. user_agent = rua()
  110. options = webdriver.ChromeOptions()
  111. # options.add_argument('--headless')
  112. options.add_argument("--no-sandbox")
  113. options.add_argument("--disable-dev-shm-usage")
  114. # options.add_argument("start-maximized")
  115. # options.add_argument('--remote-debugging-port='+str(q[2]))
  116. # options.add_argument('--remote-debugging-port=9222')
  117. # options.add_argument("--user-agent=" +user_agent)
  118. options.add_argument("--incognito")
  119. # driver = webdriver.Chrome(options=options,service=s)
  120. print('before init')
  121. # driver = webdriver.Chrome(options=options)
  122. # from tbselenium.tbdriver import TorBrowserDriver
  123. # driver=TorBrowserDriver("C:\\Users\\jared\\OneDrive\\桌面\\Tor Browser\\")
  124. # driver=TorBrowserDriver("C:/portable/tor/Tor Browser/")
  125. # driver=TorBrowserDriver("/root/tor-browser_en-US/")
  126. # driver.get('https://check.torproject.org')
  127. print('after init')
  128. driver.delete_all_cookies()
  129. # driver.set_window_size(1400,1000)
  130. print('到此')
  131. data=process_query(q)
  132. if data is not None:
  133. time.sleep(3)
  134. driver.quit()
  135. sys.exit()
  136. cursor = db.query('SELECT query FROM seo.hhh_gsc_imp where position >=2.5 and position <=8.5 order by rand() limit 1')
  137. query=None
  138. for c in cursor:
  139. query=c['query']
  140. print(query)
  141. break
  142. run_once((query,'hhh.com.tw'))