|
@@ -19,6 +19,8 @@ import traceback
|
|
|
import sys
|
|
|
import fire
|
|
|
import random
|
|
|
+import pymysql
|
|
|
+pymysql.install_as_MySQLdb()
|
|
|
|
|
|
db = dataset.connect('mysql://choozmo:pAssw0rd@db.ptt.cx:3306/seo?charset=utf8mb4')
|
|
|
table=db['general_log']
|
|
@@ -48,7 +50,6 @@ def empty_query(q):
|
|
|
|
|
|
|
|
|
def process_query(qs):
|
|
|
- print('這裡',qs)
|
|
|
q=qs[0]
|
|
|
domain=qs[1]
|
|
|
global driver
|
|
@@ -57,11 +58,17 @@ def process_query(qs):
|
|
|
driver.get(googleurl)
|
|
|
time.sleep(6)
|
|
|
|
|
|
- elmts=driver.find_elements(By.XPATH,"//div[@class='yuRUbf']/a")
|
|
|
+ while True:
|
|
|
+ try:
|
|
|
+ elmts = driver.find_elements(By.XPATH, "//div[@class='yuRUbf']/a")
|
|
|
+ print('尋找')
|
|
|
+ break
|
|
|
+ except:
|
|
|
+ pass
|
|
|
|
|
|
idx=1
|
|
|
ranking=-1
|
|
|
- print(len(elmts))
|
|
|
+ print('搜尋結果數量',len(elmts))
|
|
|
# driver.save_screenshot('c:/tmp/test.png')
|
|
|
|
|
|
for elmt in elmts:
|
|
@@ -74,7 +81,7 @@ def process_query(qs):
|
|
|
print(href)
|
|
|
print(txt)
|
|
|
print("ranking", idx)
|
|
|
- table.insert({'kw':q,'domain':domain,'ranking':idx,'title':txt,'url':href,'dt':datetime.datetime.now(),'num':'s'})
|
|
|
+ table.insert({'kw':q,'domain':domain,'ranking':idx,'title':txt,'url':href,'dt':datetime.datetime.now(),'num':1})
|
|
|
webdriver.ActionChains(driver).move_to_element(elmt).perform()
|
|
|
WebDriverWait(driver, 10).until(elmt)
|
|
|
webdriver.ActionChains(driver).move_to_element(elmt).click().perform()
|
|
@@ -85,13 +92,13 @@ def process_query(qs):
|
|
|
def run_once(q):
|
|
|
global driver
|
|
|
result=[]
|
|
|
- # s = Service('/root/driver/chromedriver')
|
|
|
- s = Service('/Users/zooeytsai/Downloads/chromedriver 2')
|
|
|
+ s = Service('/root/driver/chromedriver')
|
|
|
+ # s = Service('/Users/zooeytsai/Downloads/chromedriver 2')
|
|
|
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", "127.0.0.1:{q[2]}")
|
|
|
options.add_argument("--user-agent=" +user_agent)
|
|
|
options.add_argument("--incognito")
|
|
|
|
|
@@ -117,12 +124,18 @@ def run_once(q):
|
|
|
|
|
|
class JParams(object):
|
|
|
|
|
|
- def get(self, kw,domain):
|
|
|
+ def get(self, kw,domain,port):
|
|
|
print(kw)
|
|
|
print(domain)
|
|
|
- run_once( (kw,domain) )
|
|
|
+ run_once( (kw,domain,port) )
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
fire.Fire(JParams)
|
|
|
|
|
|
+
|
|
|
+ def get(self, kw, domain, port):
|
|
|
+ print('kw')
|
|
|
+ print(domain)
|
|
|
+ run_once((kw, domain, port))
|
|
|
+
|