root 2 年 前
コミット
2ce36ef000

+ 21 - 13
INNNews/general_clickbot.py

@@ -18,18 +18,27 @@ import time
 import traceback
 import sys
 import fire
-import pymysql
-pymysql.install_as_MySQLdb()
-from ig_tags import setting
+#import pymysql
+#pymysql.install_as_MySQLdb()
+
 
 db = dataset.connect('mysql://choozmo:pAssw0rd@db.ptt.cx:3306/seo?charset=utf8mb4')
 table=db['general_log']
-
-
-  
 driver = None
 
 
+def rua():
+    pool = [
+        "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:73.0) Gecko/20100101 Firefox/73.0",
+        "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:76.0) Gecko/20100101 Firefox/76.0",
+        "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.88 Safari/537.36"
+        "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.101 Safari/537.36",
+        "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.108 Safari/537.36",
+        "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.97 Safari/537.36",
+        "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",
+    ]
+    return random.choice(pool)
+
 
 def empty_query(q):
     global driver
@@ -47,7 +56,7 @@ def process_query(qs):
     driver.get(googleurl)
     time.sleep(6)
 
-    elmts=driver.find_elements(By.T,"//div[@class='yuRUbf']/a")
+    elmts=driver.find_elements_by_xpath("//div[@class='yuRUbf']/a")
 
     idx=1
     ranking=-1
@@ -74,11 +83,11 @@ def run_once(q):
     global driver
     result=[]
     s = Service('/root/driver/chromedriver')
-    user_agent = setting.rua()
+    user_agent = rua()
     options = webdriver.ChromeOptions()
     options.add_argument('--headless')
     options.add_argument('--remote-debugging-port=9222')
-    options.add_experimental_option("debuggerAddress", "127.0.0.1:9922")
+    options.add_experimental_option("debuggerAddress", f"127.0.0.1:{q[2]}")
     options.add_argument("--user-agent=" +user_agent)
     options.add_argument("--incognito")
 
@@ -105,10 +114,9 @@ def run_once(q):
 
 class JParams(object):
 
-  def get(self, kw,domain):
-    print('kw')
-    print(domain)
-    run_once( (kw,domain)   )
+  def get(self, kw,domain,port):
+    print('關鍵字',kw)
+    run_once( (kw,domain,port)   )
 
 
 if __name__ == '__main__':

+ 9 - 10
INNNews/run_sheet_2.py

@@ -32,9 +32,9 @@ def run_once(pport, dockername):
     domain = obj[1]
     print(kw,domain)
 
-    intval = os.system(f'python general_clickbot.py get --kw={kw} --domain={domain} --port={str(pport)}')
+    intval = os.system(f'python3 general_clickbot.py get --kw="{kw}" --domain="{domain}" --port="{str(pport)}"')
     
-    print('執行genetal_clickbot')
+    print('執行完成genetal_clickbot')
 
     if intval == -1:
         print('-1')
@@ -44,23 +44,22 @@ def run_once(pport, dockername):
 class JParams(object):
     
     def get(self, port=9222):
-        print(port)
         while True:
             try:
                 os.system('docker container restart tiny1')
-                # os.system('docker container restart tiny2')
-                # os.system('docker container restart tiny3')
+                os.system('docker container restart tiny2')
+                os.system('docker container restart tiny3')
                 time.sleep(1)
                 run_once(9922, 'tiny1')
-                # run_once(9923, 'tiny2')
-                # run_once(9924, 'tiny3')
-                
+                run_once(9923, 'tiny2')
+                run_once(9924, 'tiny3')
                 time.sleep(20)
-            
+                break           
             except:
                 os.system('docker container restart tiny1')
                 time.sleep(15)
-
+    def run(self):
+        run_once(9922,'tiny1')
 
 if __name__ == '__main__':
     fire.Fire(JParams)

+ 2 - 1
SEO/ranking_day.py

@@ -13,6 +13,7 @@ from logging.handlers import SysLogHandler
 import socket
 import pandas as pd
 import random
+from selenium.webdriver.common.by import By
 from selenium.webdriver.chrome.service import Service
 import os
 from random import randint
@@ -57,7 +58,7 @@ def process_one(item):
     # driver.save_screenshot('c:/tmp/seo/'+fname+'.png')
     # df=pd.DataFrame()
     
-    elmts = driver.find_elements_by_xpath("//div[@class='yuRUbf']/a")
+    elmts = driver.find_elements(By.XPATH,"//div[@class='yuRUbf']/a")
     cnt = 1
     datadict = {'搜尋詞': [], '結果標題': [], '結果網址': [], '結果名次': []}
     

+ 18 - 8
SEO/ranking_day2.py

@@ -13,11 +13,12 @@ import sys
 from logging.handlers import SysLogHandler
 import socket
 import pandas as pd
-import pymysql
-
-pymysql.install_as_MySQLdb()
+#import pymysql
+#pymysql.install_as_MySQLdb()
 import random
+from selenium.webdriver.common.by import By
 from selenium.webdriver.chrome.service import Service
+from selenium.webdriver.support.ui import WebDriverWait
 import os
 import fire
 
@@ -57,10 +58,11 @@ def process_one(item):
     # driver.save_screenshot('c:/tmp/seo/'+fname+'.png')
     # df=pd.DataFrame()
     
-    elmts = driver.find_elements_by_xpath("//div[@class='yuRUbf']/a")
+    elmts = driver.find_elements(By.XPATH,"//div[@class='yuRUbf']/a")
     cnt = 1
+    clickelmt=None
     datadict = {'搜尋詞': [], '結果標題': [], '結果網址': [], '結果名次': []}
-    
+    print('搜尋結果',len(elmts))
     for elmt in elmts:
         try:
             href = elmt.get_attribute('href')
@@ -70,14 +72,22 @@ def process_one(item):
             datadict['結果網址'].append(href)
             datadict['結果名次'].append(str(cnt))
             if domain in href:
+                clickelmt = elmt
                 print(href)
                 print(elmt.text)
                 table.insert(
-                    {'title': elmt.text, 'url': href, 'keyword': term, 'dt': datetime.datetime.now(), 'num': cnt})
+                    {'kw': term, 'domain': domain, 'ranking': cnt, 'title': elmt.text, 'url': href,'dt': datetime.datetime.now()})
+#                webdriver.ActionChains(driver).move_to_element(elmt).perform()
+#                webdriver.ActionChains(driver).move_to_element(elmt).click().perform()
             cnt += 1
         except:
             print('href2 exception')
             traceback.print_exc()
+    if clickelmt:
+        webdriver.ActionChains(driver).move_to_element(clickelmt).perform()
+        webdriver.ActionChains(driver).move_to_element(clickelmt).click().perform()
+        time.sleep(5)
+        print('點擊完成')
     if len(datadict['結果標題']) <= 0:
         print('None')
         driver.quit()
@@ -90,8 +100,8 @@ def process_one(item):
     # df.to_excel('/Users/zooeytsai/'+fname+".xls")
     
     driver.quit()
-    print('中場休息')
-    time.sleep(randint(90, 120))
+    print('結束')
+
 
 
 def run_once(q):

+ 12 - 13
SEO/run_ranking_day.py

@@ -5,8 +5,8 @@ import time
 import fire
 import dataset
 from random import randint
-import pymysql
-pymysql.install_as_MySQLdb()
+#import pymysql
+#pymysql.install_as_MySQLdb()
 
 def run_once(pport, dockername):
     db = dataset.connect('mysql://choozmo:pAssw0rd@db.ptt.cx:3306/seo?charset=utf8mb4')
@@ -26,17 +26,17 @@ def run_once(pport, dockername):
     if now_day_len == 0:
         print('首位')
         intval = os.system(
-            f'python3 ranking_day.py get --kw="清原 中央" --domain="taroboba-yuan.com" --port="{str(pport)}"')
+            f'python3 ranking_day2.py get --kw="清原 中央" --domain="taroboba-yuan.com" --port="{str(pport)}"')
     
     for i in lst:
         if i[0] == kw:
             id = lst.index(i)
-            intval = os.system(f'python3 ranking_day.py get --kw="{lst[id+1][0]}" --domain="{lst[id+1][1]}" --port="{str(pport)}"')
+    intval = os.system(f'python3 ranking_day2.py get --kw="{lst[id+1][0]}" --domain="{lst[id+1][1]}" --port="{str(pport)}"')
 
-        print('執行ranking_day.py')
-        if intval == -1:
-            print('-1')
-            sys.exit()
+    if intval == -1:
+        print('-1')
+        sys.exit()
+    print('執行完成ranking_day2.py')
 
 
 class JParams(object):
@@ -44,16 +44,15 @@ class JParams(object):
     def get(self, port=9222):
         while True:
             try:
-                os.system('docker container restart tiny9')
+                os.system('docker container restart tiny8')
                 time.sleep(1)
-                run_once(9928,'tiny9')
+                run_once(9929,'tiny8')
                 time.sleep(20)
                 break
             except:
                 os.system('docker container restart tiny9')
                 time.sleep(15)
 
-
-# if __name__ == '__main__':
-#     run_once()
+if __name__ == '__main__':
+    fire.Fire(JParams)
 

+ 3 - 3
monitor/general_clickbot_hhh.py

@@ -19,8 +19,8 @@ import traceback
 import sys
 import fire
 import random
-import pymysql
-pymysql.install_as_MySQLdb()
+#import pymysql
+#pymysql.install_as_MySQLdb()
 
 db = dataset.connect('mysql://choozmo:pAssw0rd@db.ptt.cx:3306/seo?charset=utf8mb4')
 table=db['general_log']
@@ -102,7 +102,7 @@ def run_once(q):
     options = webdriver.ChromeOptions()
     options.add_argument('--headless')
     options.add_argument('--remote-debugging-port=9222')
-    options.add_experimental_option("debuggerAddress", "127.0.0.1:{q[2]}")
+    options.add_experimental_option("debuggerAddress", f"127.0.0.1:{q[2]}")
     options.add_argument("--user-agent=" +user_agent)
     options.add_argument("--incognito")
 

+ 3 - 3
monitor/run_sheet_hhh.py

@@ -16,8 +16,8 @@ import redis
 import time
 import fire
 import dataset
-import pymysql
-pymysql.install_as_MySQLdb()
+#import pymysql
+#pymysql.install_as_MySQLdb()
 
 def run_once(pport, dockername):
     db = dataset.connect('mysql://choozmo:pAssw0rd@db.ptt.cx:3306/seo?charset=utf8mb4')
@@ -59,7 +59,7 @@ class JParams(object):
                 run_once(9927, 'tiny6')
 
                 time.sleep(20)
-
+                break
             except:
                 os.system('docker container restart tiny4')
                 time.sleep(15)