fast_1999.py 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. #import redis
  2. import time
  3. import traceback
  4. #import json
  5. from selenium import webdriver
  6. from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
  7. import time
  8. import os
  9. import urllib.parse
  10. from selenium.webdriver.support.ui import WebDriverWait
  11. from selenium.webdriver.common.by import By
  12. from selenium.webdriver.support import expected_conditions as EC
  13. from userAgentRandomizer import userAgents
  14. import codecs
  15. import random
  16. import requests
  17. import time
  18. import sys
  19. import docker
  20. import codecs
  21. import random
  22. import os
  23. import time
  24. import dataset
  25. driver=None
  26. headers = {
  27. "Authorization": "Bearer " + "t35vhZtWNgvDNWHc3DJh0OKll3mcB9GvC8K2EAkBug2",
  28. "Content-Type": "application/x-www-form-urlencoded"
  29. }
  30. def send_msg(kw):
  31. params = {"message": "處理關鍵字: "+kw}
  32. r = requests.post("https://notify-api.line.me/api/notify",headers=headers, params=params)
  33. def empty_query(q):
  34. global driver
  35. googleurl='https://www.google.com/search?q='+urllib.parse.quote(q)
  36. driver.get(googleurl)
  37. time.sleep(3)
  38. def process_query(qs):
  39. q=qs[0]
  40. domain=qs[2]
  41. cnt=qs[1]
  42. global driver
  43. googleurl='https://www.google.com/search?q='+urllib.parse.quote(q)
  44. driver.get(googleurl)
  45. time.sleep(3)
  46. if cnt > 0:
  47. for i in range(cnt):
  48. elmt=driver.find_element_by_xpath("//a[@id='pnnext']")
  49. webdriver.ActionChains(driver).move_to_element(elmt).perform()
  50. webdriver.ActionChains(driver).move_to_element(elmt).click().perform()
  51. time.sleep(3)
  52. elmts=driver.find_elements_by_xpath("//div[@class='g']//div[@class='yuRUbf']//a")
  53. idx=1
  54. ranking=-1
  55. for elmt in elmts:
  56. href=elmt.get_attribute('href')
  57. txt=elmt.text
  58. if len(txt)>10:
  59. # if 'hhh.com.tw' in href:
  60. # if 'hhh.com.tw' in href:
  61. # if 'ai.choozmo.com' in href:
  62. if domain in href:
  63. # if 'searchome.net' in href:
  64. webdriver.ActionChains(driver).move_to_element(elmt).perform()
  65. webdriver.ActionChains(driver).move_to_element(elmt).click().perform()
  66. break
  67. def re_get_webdriver():
  68. global driver
  69. global portnum
  70. result=[]
  71. # client = docker.from_env()
  72. # ls=client.containers.list()
  73. # print(ls)
  74. # for l in ls:
  75. # if 'p17777' in l.name:
  76. # ls[0].restart()
  77. # time.sleep(4)
  78. if driver is not None:
  79. print('closing....')
  80. # driver.quit()
  81. os.system('docker container restart p'+portnum)
  82. time.sleep(11)
  83. # options = webdriver.EdgeOptions()
  84. try:
  85. ua = userAgents()
  86. user_agent = ua.random()
  87. options = webdriver.ChromeOptions()
  88. # options.add_argument("--headless") # 視窗背後執行
  89. print(user_agent)
  90. options.add_argument("user-agent=" +user_agent)
  91. driver = webdriver.Remote(
  92. command_executor='http://127.0.0.1:'+str(portnum)+'/wd/hub'
  93. # desired_capabilities=options.to_capabilities(),
  94. )
  95. driver.set_window_size(1400,1000)
  96. return
  97. except:
  98. import traceback
  99. traceback.print_exc()
  100. driver=None
  101. return None
  102. driver=None
  103. def run_once(url):
  104. global driver
  105. i=random.randint(0,9)
  106. if i==0 or driver is None:
  107. # if True:
  108. re_get_webdriver()
  109. if driver is None:
  110. return
  111. try:
  112. driver.execute_script('window.open("'+url+'","_blank");')
  113. driver.execute_script("window.scrollTo(0, window.scrollY + 400)")
  114. time.sleep(0.5)
  115. except:
  116. print('exception')
  117. #client = docker.from_env()
  118. #ls=client.containers.list()
  119. #print(ls)
  120. #for l in ls:
  121. # if 'p18888' in l.name:
  122. # ls[0].restart()
  123. #time.sleep(4)
  124. portnum=sys.argv[1]
  125. os.system('docker restart p'+str(portnum))
  126. time.sleep(4)
  127. lst=[]
  128. db = dataset.connect('mysql://choozmo:pAssw0rd@db.ptt.cx:3306/seo?charset=utf8mb4')
  129. cursor=db.query('SELECT * FROM columnids order by rand()')
  130. for c in cursor:
  131. lst.append('https://www.hhh.com.tw/columns/detail/'+str(c['cid'])+'/')
  132. #lst=['https://www.hhh.com.tw/columns/detail/3427/index.php']
  133. #for i in range(20):
  134. while True:
  135. l=random.choice(lst)
  136. print(l)
  137. # driver.get(l)
  138. run_once(l)
  139. # time.sleep(2)