소스 검색

general_clickbot

zooeytsai 2 년 전
부모
커밋
f9bd8b7c59
2개의 변경된 파일189개의 추가작업 그리고 0개의 파일을 삭제
  1. 116 0
      INNNews/general_clickbot_local.py
  2. 73 0
      INNNews/run_sheet_local.py

+ 116 - 0
INNNews/general_clickbot_local.py

@@ -0,0 +1,116 @@
+import time
+import json
+from selenium import webdriver
+from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
+import time
+import os
+import urllib.parse
+from selenium.webdriver.support.ui import WebDriverWait
+from selenium.webdriver.common.by import By
+from selenium.webdriver.chrome.service import Service
+from selenium.webdriver.support import expected_conditions as EC
+import codecs
+import random
+import requests
+import datetime
+import dataset
+import pymysql
+pymysql.install_as_MySQLdb()
+import time
+import traceback
+import sys
+import fire
+
+
+driver = None
+
+
+def empty_query(q):
+    global driver
+    googleurl = 'https://www.google.com/search?q=' + urllib.parse.quote(q)
+    driver.get(googleurl)
+    time.sleep(3)
+
+
+def process_query(qs):
+    db = dataset.connect('mysql://choozmo:pAssw0rd@db.ptt.cx:3306/seo?charset=utf8mb4')
+    table = db['general_log']
+    q = qs[0]
+    domain = qs[1]
+    client='引新聞'
+    global driver
+    googleurl = 'https://www.google.com/search?q={}&num={}&hl={}'.format(urllib.parse.quote(q), 100, 'zh-TW')
+    print(googleurl)
+    driver.get(googleurl)
+    time.sleep(6)
+    
+    elmts = driver.find_elements(By.XPATH, "//div[@class='yuRUbf']/a")
+    
+    idx = 1
+    ranking = -1
+    print(len(elmts))
+    #    driver.save_screenshot('c:/tmp/test.png')
+    for elmt in elmts:
+        href = elmt.get_attribute('href')
+        txt = elmt.text
+        # print(txt)
+        if domain in href:
+            print(href)
+            print(txt)
+            print("ranking", idx)
+            table.insert({'kw':q,'client':client,'ranking':idx,'title':txt,'url':href,'dt':datetime.datetime.now(),'domain':domain})
+            db.close()
+            print('clicked....')
+            webdriver.ActionChains(driver).move_to_element(elmt).perform()
+            webdriver.ActionChains(driver).move_to_element(elmt).click().perform()
+            time.sleep(5)
+            break
+      
+        idx += 1
+    
+
+def run_once(q):
+    global driver
+    result = []
+    user_agent = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.88 Safari/537.36'
+    s = Service('/Users/zooeytsai/Downloads/chromedriver 4')
+    options = webdriver.ChromeOptions()
+    options.add_argument('--headless')
+    # options.add_argument('--remote-debugging-port=9222')
+    # options.add_experimental_option("debuggerAddress", "192.168.192.45:9922")
+    options.add_argument("--user-agent=" +user_agent)
+    options.add_argument("--incognito")
+    # options.add_argument('--proxy-server=socks5://172.104.93.163:41800')
+    driver = webdriver.Chrome(
+        options=options, service=s)
+    str1 = driver.capabilities['browserVersion']
+    print('版本', str1)
+    driver.delete_all_cookies()
+    driver.set_window_size(1400, 1000)
+    
+    print(q)
+    process_query(q)
+    time.sleep(3)
+    driver.quit()
+
+
+# for c in lst:ㄕ
+# while True:
+#    try:
+#        c=random.choice(lst)
+#    except:
+#        traceback.print_exc()
+#    sleepint=random.randint(320,520)
+#    time.sleep(sleepint)
+
+class JParams(object):
+    
+    def get(self, kw, domain):
+        print(kw)
+        print(domain)
+        run_once((kw, domain))
+
+
+if __name__ == '__main__':
+    fire.Fire(JParams)
+

+ 73 - 0
INNNews/run_sheet_local.py

@@ -0,0 +1,73 @@
+import sys
+import codecs
+import traceback
+import requests
+import re
+import pandas as pd
+import random
+import urllib
+import json
+import gspread
+import datetime
+from gspread_pandas import Spread, Client
+from oauth2client.service_account import ServiceAccountCredentials
+import os
+import redis
+import time
+import fire
+import dataset
+import pymysql
+pymysql.install_as_MySQLdb()
+
+def run_once():
+    db = dataset.connect('mysql://choozmo:pAssw0rd@db.ptt.cx:3306/seo?charset=utf8mb4')
+    lst = ['倉儲管理 神助移動貨架','倉儲管理 神助貨架','倉儲 管理']
+    
+    # cursor = db.query('select term, domain from seo.selected_kw where client="仁本"')
+    # for c in cursor:
+    #     lst.append([c['term'], c['domain']])
+    
+    obj = random.choice(lst)
+    kw = obj
+    domain = 'jiatetea.tw'
+    print(kw, domain)
+    os.chdir('/Users/zooeytsai/kw_tools/INNNews')
+    intval = os.system(f'python3 general_clickbot_local.py get --kw="{kw}" --domain="{domain}"')
+    
+    print('執行完成genetal_clickbot')
+    
+    if intval == -1:
+        print('-1')
+        sys.exit()
+    db.close()
+
+
+def run_specific():
+    db = dataset.connect('mysql://choozmo:pAssw0rd@db.ptt.cx:3306/seo?charset=utf8mb4')
+    # db_local = dataset.connect('mysql://root:jondae350@localhost/ig_tags')
+    lst = []
+    
+    # cursor = db.query('select term, domain from ig_tags.select_kw where client="班尼斯口碑"')
+    # for c in cursor:
+    #     lst.append([c['term'], c['domain']])
+    
+    # obj = random.choice(lst)
+    # kw = obj[0]
+    kw_list = ['倉儲 管理']
+    kw = random.choice(kw_list)
+    # domain = obj[1]
+    domain = 'gs-rack.com'
+    print(kw, domain)
+    os.chdir('/Users/zooeytsai/kw_tools/INNNews')
+    intval = os.system(f'python3 general_clickbot_local.py get --kw="{kw}" --domain="{domain}"')
+    print('執行完成genetal_clickbot')
+    
+    if intval == -1:
+        print('-1')
+        sys.exit()
+    db.close()
+    
+while True:
+    # run_once()
+    run_specific()
+    time.sleep(random.randint(120,150))