|
@@ -25,26 +25,6 @@ import redis
|
|
driver = None
|
|
driver = None
|
|
|
|
|
|
|
|
|
|
-def rua():
|
|
|
|
- pool = [
|
|
|
|
- "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:73.0) Gecko/20100101 Firefox/73.0",
|
|
|
|
- "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:76.0) Gecko/20100101 Firefox/76.0",
|
|
|
|
- "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.88 Safari/537.36"
|
|
|
|
- "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.101 Safari/537.36",
|
|
|
|
- "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.108 Safari/537.36",
|
|
|
|
- "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.97 Safari/537.36",
|
|
|
|
- "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.138 Safari/537.36 OPR/68.0.3618.125",
|
|
|
|
- ]
|
|
|
|
- return random.choice(pool)
|
|
|
|
-
|
|
|
|
-
|
|
|
|
-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):
|
|
def process_query(qs):
|
|
db = dataset.connect('mysql://choozmo:pAssw0rd@db.ptt.cx:3306/seo?charset=utf8mb4')
|
|
db = dataset.connect('mysql://choozmo:pAssw0rd@db.ptt.cx:3306/seo?charset=utf8mb4')
|
|
table=db['general_log']
|
|
table=db['general_log']
|
|
@@ -54,12 +34,12 @@ def process_query(qs):
|
|
googleurl = 'https://www.google.com/?num=100'
|
|
googleurl = 'https://www.google.com/?num=100'
|
|
driver.get(googleurl)
|
|
driver.get(googleurl)
|
|
time.sleep(6)
|
|
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 = WebDriverWait(driver,10).until(EC.presence_of_element_located((By.XPATH, '/html/body/div[1]/div[3]/form/div[1]/div[1]/div[1]/div/div[2]/input')))
|
|
send_kw_elmt.send_keys(q)
|
|
send_kw_elmt.send_keys(q)
|
|
time.sleep(3)
|
|
time.sleep(3)
|
|
send_kw_elmt.send_keys(Keys.ENTER)
|
|
send_kw_elmt.send_keys(Keys.ENTER)
|
|
time.sleep(6)
|
|
time.sleep(6)
|
|
-
|
|
|
|
|
|
+ print(driver.current_url)
|
|
elmts=driver.find_elements(By.XPATH,"//div[@class='yuRUbf']/a")
|
|
elmts=driver.find_elements(By.XPATH,"//div[@class='yuRUbf']/a")
|
|
|
|
|
|
idx=1
|
|
idx=1
|
|
@@ -76,55 +56,57 @@ def process_query(qs):
|
|
table.insert(
|
|
table.insert(
|
|
{'kw': q, 'domain': domain, 'ranking': idx, 'title': txt, 'url': href, 'dt': datetime.datetime.now()})
|
|
{'kw': q, 'domain': domain, 'ranking': idx, 'title': txt, 'url': href, 'dt': datetime.datetime.now()})
|
|
webdriver.ActionChains(driver).move_to_element(elmts[0]).perform()
|
|
webdriver.ActionChains(driver).move_to_element(elmts[0]).perform()
|
|
|
|
+ time.sleep(3)
|
|
webdriver.ActionChains(driver).move_to_element(elmts[0]).click().perform()
|
|
webdriver.ActionChains(driver).move_to_element(elmts[0]).click().perform()
|
|
time.sleep(5)
|
|
time.sleep(5)
|
|
- for elmt in elmts:
|
|
|
|
- href=elmt.get_attribute('href')
|
|
|
|
- txt=elmt.text
|
|
|
|
- if len(txt)>10:
|
|
|
|
- if domain in href:
|
|
|
|
- print('clicked....')
|
|
|
|
- print('點擊網址',href)
|
|
|
|
- print('標題',txt)
|
|
|
|
- print("ranking", idx)
|
|
|
|
- table.insert({'kw':q,'domain':domain,'ranking':idx,'title':txt,'url':href,'dt':datetime.datetime.now()})
|
|
|
|
- webdriver.ActionChains(driver).move_to_element(elmt).perform()
|
|
|
|
- webdriver.ActionChains(driver).move_to_element(elmt).click().perform()
|
|
|
|
- time.sleep(5)
|
|
|
|
- break
|
|
|
|
- idx+=1
|
|
|
|
|
|
+ else:
|
|
|
|
+ for elmt in elmts:
|
|
|
|
+ href=elmt.get_attribute('href')
|
|
|
|
+ txt=elmt.text
|
|
|
|
+ if len(txt)>10:
|
|
|
|
+ if domain in href:
|
|
|
|
+ print('clicked....')
|
|
|
|
+ print('點擊網址',href)
|
|
|
|
+ print('標題',txt)
|
|
|
|
+ print("ranking", idx)
|
|
|
|
+ webdriver.ActionChains(driver).move_to_element(elmt).perform()
|
|
|
|
+ time.sleep(3)
|
|
|
|
+ webdriver.ActionChains(driver).move_to_element(elmt).click().perform()
|
|
|
|
+ table.insert({'kw':q,'domain':domain,'ranking':idx,'title':txt,'url':href,'dt':datetime.datetime.now()})
|
|
|
|
+ time.sleep(5)
|
|
|
|
+ break
|
|
|
|
+ idx+=1
|
|
db.close()
|
|
db.close()
|
|
|
|
+ print('資料庫關閉')
|
|
|
|
+ driver.quit()
|
|
|
|
+
|
|
def run_once(q):
|
|
def run_once(q):
|
|
global driver
|
|
global driver
|
|
- result=[]
|
|
|
|
s = Service('/root/driver/chromedriver')
|
|
s = Service('/root/driver/chromedriver')
|
|
- user_agent = rua()
|
|
|
|
options = webdriver.ChromeOptions()
|
|
options = webdriver.ChromeOptions()
|
|
options.add_argument('--headless')
|
|
options.add_argument('--headless')
|
|
options.add_argument('--remote-debugging-port=9222')
|
|
options.add_argument('--remote-debugging-port=9222')
|
|
- options.add_experimental_option("debuggerAddress", f"127.0.0.1:{q[2]}")
|
|
|
|
|
|
+ options.add_experimental_option("debuggerAddress",f"127.0.0.1:{q[2]}")
|
|
# options.add_argument("--user-agent=" +user_agent)
|
|
# options.add_argument("--user-agent=" +user_agent)
|
|
options.add_argument("--incognito")
|
|
options.add_argument("--incognito")
|
|
- if 'sorry' in driver.current_url:
|
|
|
|
- r = redis.Redis(host='db.ptt.cx', port=6379, db=2,password='choozmo9')
|
|
|
|
- data=r.get('google_proxy')
|
|
|
|
- jstext=data.decode('utf-8')
|
|
|
|
- jsobj=json.loads(jstext)
|
|
|
|
- print('Free proxy',jsobj)
|
|
|
|
- proxy=random.choice(jsobj)
|
|
|
|
- change_ip_list = ['--proxy-server=%s' % proxy,"--proxy-server=socks5://127.0.0.1:9050","--proxy-server=socks5://192.53.174.202:8180"]
|
|
|
|
- change_ip = random.choice(change_ip_list)
|
|
|
|
- options.add_argument(change_ip)
|
|
|
|
- print('使用代理ip',change_ip)
|
|
|
|
-
|
|
|
|
|
|
+ r = redis.Redis(host='db.ptt.cx', port=6379, db=2,password='choozmo9')
|
|
|
|
+ data=r.get('google_proxy')
|
|
|
|
+ jstext=data.decode('utf-8')
|
|
|
|
+ jsobj=json.loads(jstext)
|
|
|
|
+ #print('Free proxy',jsobj)
|
|
|
|
+ proxy=random.choice(jsobj)
|
|
|
|
+ i5 = "--proxy-server=socks5://172.104.93.163:41800"
|
|
|
|
+ change_ip_list = ['--proxy-server=%s' % proxy,"--proxy-server=socks5://127.0.0.1:9050","--proxy-server=socks5://192.53.174.202:8180"]
|
|
|
|
+ change_ip = random.choice(change_ip_list)
|
|
|
|
+ options.add_argument(i5)
|
|
|
|
+ #print('使用代理ip',change_ip)
|
|
|
|
+
|
|
driver = webdriver.Chrome(options=options,service=s)
|
|
driver = webdriver.Chrome(options=options,service=s)
|
|
|
|
|
|
driver.delete_all_cookies()
|
|
driver.delete_all_cookies()
|
|
driver.set_window_size(1400,1000)
|
|
driver.set_window_size(1400,1000)
|
|
|
|
|
|
process_query(q)
|
|
process_query(q)
|
|
- time.sleep(3)
|
|
|
|
- driver.quit()
|
|
|
|
|
|
|
|
|
|
|
|
#for c in lst:
|
|
#for c in lst:
|