test_wd.py 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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. from selenium_stealth import stealth
  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 random
  23. import pymysql
  24. pymysql.install_as_MySQLdb()
  25. db = dataset.connect('mysql://choozmo:pAssw0rd@db.ptt.cx:3306/seo?charset=utf8mb4')
  26. table=db['general_log']
  27. driver = None
  28. headers = {
  29. "Authorization": "Bearer " + "6SDULL1Ebklduc6TFxa97AFto5Sj21kyJ30CxiLiSoi",
  30. "Content-Type": "application/x-www-form-urlencoded"
  31. }
  32. def rua():
  33. pool = [
  34. "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:73.0) Gecko/20100101 Firefox/73.0",
  35. "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:76.0) Gecko/20100101 Firefox/76.0",
  36. "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.88 Safari/537.36"
  37. "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.101 Safari/537.36",
  38. "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.108 Safari/537.36",
  39. "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.97 Safari/537.36",
  40. "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",
  41. ]
  42. return random.choice(pool)
  43. def empty_query(q):
  44. global driver
  45. googleurl='https://www.google.com/search?q='+urllib.parse.quote(q)
  46. driver.get(googleurl)
  47. time.sleep(3)
  48. def process_query(qs):
  49. q=qs[0]
  50. domain=qs[1]
  51. global driver
  52. googleurl = 'https://www.google.com/search?q={}&num={}&hl={}'.format(urllib.parse.quote(q), 100,'zh-TW')
  53. print(googleurl)
  54. # stealth(driver,
  55. # languages=["en-US", "en"],
  56. # vendor="Google Inc.",
  57. # platform="Win32",
  58. # webgl_vendor="Intel Inc.",
  59. # renderer="Intel Iris OpenGL Engine",
  60. # fix_hairline=True,
  61. # )
  62. driver.get(googleurl)
  63. time.sleep(6)
  64. while True:
  65. try:
  66. elmts = driver.find_elements(By.XPATH, "//div[@class='yuRUbf']/a")
  67. print('尋找')
  68. break
  69. except:
  70. pass
  71. idx=1
  72. ranking=-1
  73. lenelmts=len(elmts)
  74. print('搜尋結果數量',lenelmts)
  75. if lenelmts <=0:
  76. os.system('python3 tplink_reboot.py')
  77. time.sleep(200)
  78. os.system('netsh wlan connect TP-Link_78E0')
  79. # os.system('docker container restart p4444')
  80. return
  81. # if len(elmts) <=0:
  82. # send_msg('network failed...')
  83. # driver.save_screenshot('c:/tmp/test.png')
  84. for elmt in elmts:
  85. href=elmt.get_attribute('href')
  86. txt=elmt.text
  87. if len(txt)>10:
  88. if domain in href:
  89. print('clicked....')
  90. print(href)
  91. print(txt)
  92. print("ranking", idx)
  93. table.insert({'kw':q,'domain':domain,'ranking':idx,'title':txt,'url':href,'dt':datetime.datetime.now(),'num':1})
  94. webdriver.ActionChains(driver).move_to_element(elmt).perform()
  95. webdriver.ActionChains(driver).move_to_element(elmt).click().perform()
  96. break
  97. idx+=1
  98. def run_once(q):
  99. global driver
  100. result=[]
  101. os.system('docker container restart p4444')
  102. time.sleep(12)
  103. user_agent = rua()
  104. options = webdriver.ChromeOptions()
  105. options.add_argument('--headless')
  106. options.add_argument("--no-sandbox")
  107. options.add_argument("--disable-dev-shm-usage")
  108. options.add_argument('--disable-blink-features=AutomationControlled')
  109. # options.add_argument("--user-agent=" +user_agent)
  110. # options.add_argument("--incognito")
  111. driver = webdriver.Remote(
  112. command_executor='http://127.0.0.1:4444/wd/hub',
  113. options=options
  114. )
  115. driver.delete_all_cookies()
  116. driver.set_window_size(1400,1000)
  117. print('到此')
  118. process_query(q)
  119. time.sleep(3)
  120. driver.quit()
  121. # sys.exit()
  122. cursor = db.query('SELECT query FROM seo.hhh_gsc_imp where position >=2.5 and position <=8.5 order by rand() limit 500;')
  123. lst=[]
  124. for c in cursor:
  125. lst.append(c['query'])
  126. while True:
  127. kw=random.choice(lst)
  128. print(kw)
  129. run_once((kw, 'hhh.com.tw', 4444))
  130. #class JParams(object):
  131. #
  132. # def get(self, kw, domain, port):
  133. # print(kw)
  134. # print(domain)
  135. # run_once((kw, domain, port))
  136. #if __name__ == '__main__':
  137. # fire.Fire(JParams)