|
@@ -28,6 +28,7 @@ import traceback
|
|
|
import time
|
|
|
from selenium.webdriver.common.keys import Keys
|
|
|
from selenium.webdriver.common.by import By
|
|
|
+from selenium.webdriver.chrome.service import Service
|
|
|
# import pymysql
|
|
|
# pymysql.install_as_MySQLdb()
|
|
|
|
|
@@ -50,7 +51,7 @@ app.add_middleware(
|
|
|
db = dataset.connect('mysql://choozmo:pAssw0rd@db.ptt.cx:3306/gtrends?charset=utf8mb4')
|
|
|
|
|
|
# app.mount("/web", StaticFiles(directory="static"), name="static")
|
|
|
-app.mount("/web", StaticFiles(directory="/Users/zooeytsai/kw_tools/web/static"), name="static")
|
|
|
+app.mount("/web", StaticFiles(directory="/root/src/kw_tools/web/static"), name="static")
|
|
|
|
|
|
def thread_function(kw):
|
|
|
global db
|
|
@@ -127,56 +128,83 @@ async def func_expand(kw: str = Form(...),kw2:Optional[str] = Form(None),kw3:Opt
|
|
|
return RedirectResponse(url="/tree_list",status_code=302)
|
|
|
# return HTMLResponse('<html><head><meta http-equiv="refresh" content="0; URL="/tree_list" /></head></html>')
|
|
|
|
|
|
-
|
|
|
-@app.post("/ranking/")
|
|
|
-async def ranking(kw: str = Form(...), domain:str = Form(...)):
|
|
|
+
|
|
|
+def restart_browser():
|
|
|
global driver
|
|
|
- options = webdriver.ChromeOptions()
|
|
|
- options.add_argument("--no-sandbox")
|
|
|
- options.add_argument("--disable-dev-shm-usage")
|
|
|
- options.add_argument('--headless')
|
|
|
- driver = webdriver.Chrome(options=options)
|
|
|
- driver.set_window_size(950, 20000)
|
|
|
-
|
|
|
- # db = dataset.connect('mysql://choozmo:pAssw0rd@db.ptt.cx:3306/seo?charset=utf8mb4')
|
|
|
- db_local = dataset.connect('mysql://root:jondae350@localhost/ig_tags')
|
|
|
- table = db_local['google_rank']
|
|
|
-
|
|
|
- # escaped_search_term=urllib.parse.quote(term)
|
|
|
- googleurl = 'https://www.google.com/?num=100'
|
|
|
- driver.get(googleurl)
|
|
|
- time.sleep(6)
|
|
|
- send_kw_elmt = driver.find_element(By.XPATH,
|
|
|
- '/html/body/div[1]/div[3]/form/div[1]/div[1]/div[1]/div/div[2]/input')
|
|
|
- send_kw_elmt.send_keys(kw)
|
|
|
- time.sleep(3)
|
|
|
- send_kw_elmt.send_keys(Keys.ENTER)
|
|
|
- time.sleep(6)
|
|
|
- elmts = driver.find_elements_by_xpath("//div[@class='yuRUbf']/a")
|
|
|
-
|
|
|
- cnt = 1
|
|
|
- datadict = {'搜尋詞': [], '結果標題': [], '結果網址': [], '結果名次': []}
|
|
|
-
|
|
|
- for elmt in elmts:
|
|
|
+ if driver is not None:
|
|
|
+ print('closing')
|
|
|
+ driver.quit()
|
|
|
+ driver = None
|
|
|
+ try:
|
|
|
+ options = webdriver.ChromeOptions()
|
|
|
+ options.add_argument("--no-sandbox")
|
|
|
+ options.add_argument("--disable-dev-shm-usage")
|
|
|
+ options.add_argument('--headless')
|
|
|
+ # options.add_experimental_option("debuggerAddress", "127.0.0.1:9923")
|
|
|
+ options.add_argument("--incognito")
|
|
|
try:
|
|
|
- href = elmt.get_attribute('href')
|
|
|
- if domain in href:
|
|
|
- datadict['搜尋詞'].append(kw)
|
|
|
- datadict['結果標題'].append(elmt.text)
|
|
|
- datadict['結果網址'].append(href)
|
|
|
- datadict['結果名次'].append(str(cnt))
|
|
|
- cnt += 1
|
|
|
+ driver = webdriver.Chrome(options=options,executable_path='/root/driver/chromedriver103')
|
|
|
+ #driver = webdriver.Remote(command_executor='http://127.0.0.1:'+str(portnum)+'/wd/hub',options=options)
|
|
|
except:
|
|
|
- print('href2 exception')
|
|
|
- traceback.print_exc()
|
|
|
+ return None
|
|
|
+ except:
|
|
|
+ print('開啟失敗')
|
|
|
+ driver=None
|
|
|
+ return None
|
|
|
+ return driver
|
|
|
+
|
|
|
+@app.post("/ranking/")
|
|
|
+async def ranking(kw: str = Form(...), domain:str = Form(...),kw2:Optional[str] = Form(None),domain2:Optional[str] = Form(None),kw3:Optional[str] = Form(None),domain3:Optional[str] = Form(None),kw4:Optional[str] = Form(None),domain4:Optional[str] = Form(None),kw5:Optional[str] = Form(None),domain5:Optional[str] = Form(None)):
|
|
|
+ kwlst = []
|
|
|
+ kwlst.append([kw,domain])
|
|
|
+ if kw2 is not None:
|
|
|
+ kwlst.append([kw2,domain2])
|
|
|
+ if kw3 is not None:
|
|
|
+ kwlst.append([kw3,domain3])
|
|
|
+ if kw4 is not None:
|
|
|
+ kwlst.append([kw4,domain4])
|
|
|
+ if kw5 is not None:
|
|
|
+ kwlst.append([kw5,domain5])
|
|
|
+ result = []
|
|
|
+ for i in kwlst:
|
|
|
+ driver = restart_browser()
|
|
|
+ # escaped_search_term=urllib.parse.quote(term)
|
|
|
+ googleurl = 'https://www.google.com/?num=100'
|
|
|
+ driver.get(googleurl)
|
|
|
+ time.sleep(6)
|
|
|
+ send_kw_elmt = driver.find_element(By.XPATH,
|
|
|
+ '/html/body/div[1]/div[3]/form/div[1]/div[1]/div[1]/div/div[2]/input')
|
|
|
+ send_kw_elmt.send_keys(i[0])
|
|
|
+ time.sleep(3)
|
|
|
+ send_kw_elmt.send_keys(Keys.ENTER)
|
|
|
+ time.sleep(6)
|
|
|
+ elmts = driver.find_elements_by_xpath("//div[@class='yuRUbf']/a")
|
|
|
+
|
|
|
+ cnt = 1
|
|
|
+ datadict = {'搜尋詞': [], '結果標題': [], '結果網址': [], '結果名次': []}
|
|
|
+ domain_name = i[1]
|
|
|
+ for elmt in elmts:
|
|
|
+ try:
|
|
|
+ href = elmt.get_attribute('href')
|
|
|
+ if domain_name in href:
|
|
|
+ datadict['搜尋詞'].append(i[0])
|
|
|
+ datadict['結果標題'].append(elmt.text)
|
|
|
+ datadict['結果網址'].append(href)
|
|
|
+ datadict['結果名次'].append(str(cnt))
|
|
|
+ cnt += 1
|
|
|
+ except:
|
|
|
+ print('href2 exception')
|
|
|
+ traceback.print_exc()
|
|
|
+ result.append(datadict)
|
|
|
+ print(domain_name)
|
|
|
+ print(datadict)
|
|
|
+ driver.quit()
|
|
|
+ print('數量',len(elmts))
|
|
|
+ time.sleep(90)
|
|
|
|
|
|
- driver.quit()
|
|
|
- time.sleep(60)
|
|
|
- # db.close()
|
|
|
- db_local.close()
|
|
|
# return "ok"
|
|
|
# return RedirectResponse(url="/ranking_result",)
|
|
|
- html = "<html><body><h2>"+str(datadict)+"</br></br>"
|
|
|
+ html = f"<html><body>{result}</body></html>"
|
|
|
return html
|
|
|
|
|
|
@app.get("/ranking_result/")
|
|
@@ -185,4 +213,4 @@ async def tree_list():
|
|
|
|
|
|
# html += "<tr><td>" + c['kw'] + "</td>"
|
|
|
|
|
|
- return html
|
|
|
+ return html
|