zooeytsai 2 years ago
parent
commit
f41a6d6779
1 changed files with 10 additions and 7 deletions
  1. 10 7
      SEO/ranking_day.py

+ 10 - 7
SEO/ranking_day.py

@@ -23,6 +23,9 @@ import os
 path = 'C:\portable\chromedriver'
 path_z = '/Users/zooeytsai/Downloads/chromedriver 2'
 driver = None
+db = dataset.connect('mysql://choozmo:pAssw0rd@db.ptt.cx:3306/seo?charset=utf8mb4')
+lst = []
+table = db['google_rank']
 
 
 def rua():
@@ -60,13 +63,14 @@ def process_one(item):
         for elmt in elmts:
             try:
                 href = elmt.get_attribute('href')
-                print(href)
-                print(elmt.text)
+
                 datadict['搜尋詞'].append(term)
                 datadict['結果標題'].append(elmt.text)
                 datadict['結果網址'].append(href)
                 datadict['結果名次'].append(str(cnt))
                 if domain in href:
+                    print(href)
+                    print(elmt.text)
                     table.insert(
                         {'title': elmt.text, 'url': href, 'keyword': term, 'dt': datetime.datetime.now(), 'num': cnt})
                 cnt += 1
@@ -97,7 +101,7 @@ def run_once(pport,item):
     options = webdriver.ChromeOptions()
     options.add_argument('--headless')
     options.add_argument('--remote-debugging-port=9222')
-    options.add_experimental_option(f"debuggerAddress", "127.0.0.1:{pport}}")
+    options.add_experimental_option("debuggerAddress", f"127.0.0.1:{pport}")
     options.add_argument("--user-agent=" + user_agent)
     options.add_argument("--incognito")
     
@@ -110,11 +114,8 @@ def run_once(pport,item):
     time.sleep(3)
     driver.quit()
     
-db = dataset.connect('mysql://choozmo:pAssw0rd@db.ptt.cx:3306/seo?charset=utf8mb4')
-lst = []
-table = db['google_rank']
-cursor = db.query('select term,domain from seo.selected_kw')
 
+cursor = db.query('select term,domain from seo.selected_kw')
 for c in cursor:
     lst.append([c['term'],c['domain']])
 for i in lst:
@@ -127,7 +128,9 @@ for i in lst:
             os.system('docker container restart tiny9')
             time.sleep(1)
             run_once(9928,i)
+            print('docker開啟完成')
             time.sleep(randint(150,180))
+            break
         except:
             os.system('docker container restart tiny9')
             time.sleep(15)