zooeytsai 3 lat temu
rodzic
commit
92fe350273
1 zmienionych plików z 18 dodań i 16 usunięć
  1. 18 16
      SEO/ranking_day.py

+ 18 - 16
SEO/ranking_day.py

@@ -1,4 +1,3 @@
-from random import randint
 import sys
 import dataset
 from selenium import webdriver
@@ -13,12 +12,10 @@ import sys
 from logging.handlers import SysLogHandler
 import socket
 import pandas as pd
-import pymysql
-pymysql.install_as_MySQLdb()
 import random
 from selenium.webdriver.chrome.service import Service
 import os
-
+from random import randint
 
 path = 'C:\portable\chromedriver'
 path_z = '/Users/zooeytsai/Downloads/chromedriver 2'
@@ -40,13 +37,14 @@ def rua():
     ]
     return random.choice(pool)
 
+
 def process_one(item):
     global driver
-
+    
     term = item[0]
     domain = item[1]
-    print(term,domain)
-
+    print(term, domain)
+    
     escaped_search_term = urllib.parse.quote(term)
     googleurl = 'https://www.google.com/search?q={}&num={}&hl={}'.format(escaped_search_term, 100, 'zh-TW')
     print(googleurl)
@@ -60,10 +58,15 @@ def process_one(item):
     cnt = 1
     datadict = {'搜尋詞': [], '結果標題': [], '結果網址': [], '結果名次': []}
     
+    if len(elmts) == 0:
+        print('chrome異常')
+        os.chdir('/root')
+        os.system('python3 reboot.py')
+    
     for elmt in elmts:
         try:
             href = elmt.get_attribute('href')
-
+            
             datadict['搜尋詞'].append(term)
             datadict['結果標題'].append(elmt.text)
             datadict['結果網址'].append(href)
@@ -93,7 +96,7 @@ def process_one(item):
     time.sleep(randint(90, 120))
 
 
-def run_once(pport,item):
+def run_once(pport, item):
     global driver
     result = []
     s = Service('/root/driver/chromedriver')
@@ -113,24 +116,23 @@ def run_once(pport,item):
     process_one(item)
     time.sleep(3)
     driver.quit()
-    
+
 
 cursor = db.query('select term,domain from seo.selected_kw')
 for c in cursor:
-    lst.append([c['term'],c['domain']])
-
+    lst.append([c['term'], c['domain']])
 
 for i in lst:
-    print(i)
+    print('這裡', i)
     while True:
         try:
             os.system('docker container restart tiny9')
             time.sleep(1)
-            run_once(9928,i)
+            run_once(9928, i)
             print('docker開啟完成')
-            time.sleep(randint(150,180))
             break
         except:
             os.system('docker container restart tiny9')
             time.sleep(15)
-
+    print('等待進行下一個關鍵字')
+    time.sleep(randint(180, 240))