|
@@ -25,6 +25,7 @@ import requests
|
|
|
import pymysql
|
|
|
import urllib.parse
|
|
|
pymysql.install_as_MySQLdb()
|
|
|
+from userAgentRandomizer import userAgents
|
|
|
driver=None
|
|
|
|
|
|
db = dataset.connect('mysql://choozmo:pAssw0rd@db.ptt.cx:3306/seo?charset=utf8mb4')
|
|
@@ -65,9 +66,11 @@ def re_get_webdriver():
|
|
|
return None
|
|
|
|
|
|
def getDriver():
|
|
|
+ ua=userAgents().random()
|
|
|
options = webdriver.ChromeOptions()
|
|
|
- #options.add_argument("user-agent=%s" % rua())
|
|
|
- options.add_argument('--headless')
|
|
|
+ #print(ua)
|
|
|
+ #options.add_argument("user-agent="+ua)
|
|
|
+ #options.add_argument('--headless')
|
|
|
options.add_argument('--incognito')
|
|
|
options.add_argument('--no-sandbox')
|
|
|
driver=webdriver.Chrome(options=options)
|
|
@@ -101,6 +104,7 @@ def run_once(jsobj):
|
|
|
print(driver.current_url)
|
|
|
if 'sorry' in driver.current_url:
|
|
|
print("URL Error: Caught")
|
|
|
+ driver.quit()
|
|
|
return
|
|
|
# elmt = driver.find_element(By.XPATH, "//input[@name='q']")
|
|
|
# time.sleep(1)
|
|
@@ -113,8 +117,8 @@ def run_once(jsobj):
|
|
|
numresults=len(elmts)
|
|
|
print('搜尋結果數量',numresults)
|
|
|
if numresults==0:
|
|
|
- send_msg('stop working...')
|
|
|
- sys.exit()
|
|
|
+ driver.quit()
|
|
|
+ return
|
|
|
|
|
|
idx=1
|
|
|
found=False
|
|
@@ -227,10 +231,22 @@ while True:
|
|
|
code='03'
|
|
|
|
|
|
run_once({'domain':domain,'kw':kw, 'cust':'啟翔'})
|
|
|
- time.sleep(61)
|
|
|
+ if driver is not None:
|
|
|
+ try:
|
|
|
+ driver.quit()
|
|
|
+ except:
|
|
|
+ pass
|
|
|
cursor=None
|
|
|
driver=None
|
|
|
+ time.sleep(61)
|
|
|
except:
|
|
|
traceback.print_exc()
|
|
|
print("Execution Error")
|
|
|
+ if driver is not None:
|
|
|
+ try:
|
|
|
+ driver.quit()
|
|
|
+ except:
|
|
|
+ pass
|
|
|
+ cursor=None
|
|
|
+ driver=None
|
|
|
time.sleep(20)
|