123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304 |
- #import redis
- import time
- import traceback
- #import json
- from selenium import webdriver
- from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
- import time
- import os
- from selenium.webdriver.support.ui import WebDriverWait
- from selenium.webdriver.common.by import By
- from selenium.webdriver.support import expected_conditions as EC
- import dataset
- from selenium.webdriver.common.keys import Keys
- import json
- import random
- import time
- import redis
- import sys
- import codecs
- import random
- import os
- import time
- from userAgentRandomizer import userAgents
- driver=None
- def re_get_webdriver():
- global driver
- result=[]
- if driver is not None:
- print('closing....')
- driver.quit()
- os.system('killall chrome')
- print('quit....')
- driver=None
- try:
- ua = userAgents()
- user_agent = ua.random()
- options = webdriver.ChromeOptions()
- options.add_argument("--no-sandbox")
- options.add_argument("--disable-dev-shm-usage")
- options.add_argument("--headless")
- print(user_agent)
- # options.add_argument("--user-agent=" +user_agent)
- options.add_argument("--incognito")
- driver=None
- try:
- driver = webdriver.Chrome(options=options)
- except:
- # driver.quit()
- # os.system('pkill -f ')
- os.system('kill %d' % os.getpid())
- sys.exit()
- return
- driver.set_window_size(1400,1000)
- return
- except:
- import traceback
- traceback.print_exc()
- driver=None
- return None
- def from_shopping(kw):
- global driver
- driver.get('https://shopping.google.com')
- time.sleep(5)
- elmt = driver.find_element(By.XPATH, "//input[@id='REsRA']")
- elmt.send_keys('幸福空間')
- elmt.send_keys(Keys.ENTER) #hits space
- time.sleep(7)
- elmt = driver.find_element(By.XPATH, "//div[@class='hdtb-mitem']/a[contains(text(),'全部') or contains(text(),'All')]")
- webdriver.ActionChains(driver).move_to_element(elmt).perform()
- webdriver.ActionChains(driver).move_to_element(elmt).click().perform()
- time.sleep(5)
- def from_book(kw):
- global driver
- driver.get('https://books.google.com/')
- time.sleep(5)
- elmt = driver.find_element(By.XPATH, "//input[@id='oc-search-input']")
- elmt.send_keys('幸福空間')
- elmt.send_keys(Keys.ENTER) #hits space
- time.sleep(7)
- elmt = driver.find_element(By.XPATH, "//div[@class='hdtb-mitem']/a[contains(text(),'全部') or contains(text(),'All')]")
- webdriver.ActionChains(driver).move_to_element(elmt).perform()
- webdriver.ActionChains(driver).move_to_element(elmt).click().perform()
- time.sleep(5)
- def from_wiki(kw):
- global driver
- driver.get('https://en.wikipedia.org/wiki/Google_Search')
- time.sleep(4)
- elmt = driver.find_element(By.XPATH, "//a[contains(@href,'https://www.google.com/')]")
- webdriver.ActionChains(driver).move_to_element(elmt).perform()
- webdriver.ActionChains(driver).move_to_element(elmt).click().perform()
- time.sleep(5)
- elmt = driver.find_element(By.XPATH, "//input[@class='gLFyf gsfi']")
- time.sleep(1)
- elmt.send_keys(kw)
- elmt.send_keys(Keys.ENTER)
- time.sleep(6)
- def from_bing(kw):
- global driver
- driver.get('https://www.bing.com/search?q=google')
- time.sleep(4)
- elmt = driver.find_element(By.XPATH, "//a[contains(@href,'https://www.google.com/')]")
- webdriver.ActionChains(driver).move_to_element(elmt).perform()
- webdriver.ActionChains(driver).move_to_element(elmt).click().perform()
- time.sleep(5)
- elmt = driver.find_element(By.XPATH, "//input[@class='gLFyf gsfi']")
- time.sleep(1)
- elmt.send_keys(kw)
- elmt.send_keys(Keys.ENTER)
- time.sleep(6)
- def from_ecosia(kw):
- global driver
- driver.get('https://www.ecosia.org/search?method=index&q=GOOGLE')
- time.sleep(4)
- elmt = driver.find_element(By.XPATH, "//a[contains(@href,'https://www.google.com/')]")
- webdriver.ActionChains(driver).move_to_element(elmt).perform()
- webdriver.ActionChains(driver).move_to_element(elmt).click().perform()
- time.sleep(5)
- elmt = driver.find_element(By.XPATH, "//input[@class='gLFyf gsfi']")
- time.sleep(1)
- elmt.send_keys(kw)
- elmt.send_keys(Keys.ENTER)
- time.sleep(6)
- def from_brave(kw):
- global driver
- driver.get('https://search.brave.com/search?q=google&source=web')
- time.sleep(4)
- elmt = driver.find_element(By.XPATH, "//a[contains(@href,'https://www.google.com/')]")
- webdriver.ActionChains(driver).move_to_element(elmt).perform()
- webdriver.ActionChains(driver).move_to_element(elmt).click().perform()
- time.sleep(5)
- elmt = driver.find_element(By.XPATH, "//input[@class='gLFyf gsfi']")
- time.sleep(1)
- elmt.send_keys(kw)
- elmt.send_keys(Keys.ENTER)
- time.sleep(6)
- def from_duckduckgo(kw):
- global driver
- driver.get('https://duckduckgo.com/?q=google')
- time.sleep(4)
- elmt = driver.find_element(By.XPATH, "//a[contains(@href,'https://www.google.com/')]")
- webdriver.ActionChains(driver).move_to_element(elmt).perform()
- webdriver.ActionChains(driver).move_to_element(elmt).click().perform()
- time.sleep(5)
- # time.sleep(9999)
- elmt = driver.find_element(By.XPATH, "//input[@class='gLFyf gsfi']")
- time.sleep(1)
- elmt.send_keys(kw)
- elmt.send_keys(Keys.ENTER)
- time.sleep(6)
- def from_ekoru(kw):
- global driver
- driver.get('https://www.ekoru.org/?q=google')
- time.sleep(4)
- elmt = driver.find_element(By.XPATH, "//a[contains(@href,'https://www.google.com/')]")
- webdriver.ActionChains(driver).move_to_element(elmt).perform()
- webdriver.ActionChains(driver).move_to_element(elmt).click().perform()
- time.sleep(5)
- # time.sleep(9999)
- elmt = driver.find_element(By.XPATH, "//input[@class='gLFyf gsfi']")
- time.sleep(1)
- elmt.send_keys(kw)
- elmt.send_keys(Keys.ENTER)
- time.sleep(6)
- def from_yahoo(kw):
- global driver
- driver.get('https://search.yahoo.com/search?p=google')
- time.sleep(4)
- elmt = driver.find_element(By.XPATH, "//a[contains(@href,'https://www.google.com/')]")
- webdriver.ActionChains(driver).move_to_element(elmt).perform()
- webdriver.ActionChains(driver).move_to_element(elmt).click().perform()
- time.sleep(2)
- driver.switch_to.window(driver.window_handles[1])
- time.sleep(3)
- print(driver.current_url)
- elmt = driver.find_element(By.XPATH, "//input[@name='q']")
- time.sleep(1)
- elmt.send_keys(kw)
- elmt.send_keys(Keys.ENTER)
- time.sleep(6)
- def from_gibiru(kw):
- global driver
- driver.get('https://gibiru.com/results.html?q=google')
- time.sleep(4)
- elmt = driver.find_element(By.XPATH, "//a[contains(@href,'https://www.google.com/')]")
- webdriver.ActionChains(driver).move_to_element(elmt).perform()
- webdriver.ActionChains(driver).move_to_element(elmt).click().perform()
- time.sleep(2)
- driver.switch_to.window(driver.window_handles[1])
- time.sleep(3)
- print(driver.current_url)
- elmt = driver.find_element(By.XPATH, "//input[@name='q']")
- time.sleep(1)
- elmt.send_keys(kw)
- elmt.send_keys(Keys.ENTER)
- time.sleep(6)
- def run_once(jsobj):
- global driver
- i=random.randint(0,9)
- if driver is None:
- time.sleep(8)
- re_get_webdriver()
- if driver is None:
- return
- try:
- kw=jsobj['kw']
- domain=jsobj['domain']
- # kw='leo 娛樂城 484'
- # kw='leo 娛樂城 3011'
- # domain='tha484.com'
- # domain='tha3011.com'
- if i==0:
- from_book(kw)
- elif i==1:
- from_shopping(kw)
- elif i==2:
- from_wiki(kw)
- elif i==3:
- from_bing(kw)
- elif i==4:
- from_duckduckgo(kw)
- elif i==5:
- from_yahoo(kw)
- elif i==6:
- from_gibiru(kw)
- elif i==7:
- from_ekoru(kw)
- elif i==8:
- from_ecosia(kw)
- elif i==9:
- from_brave(kw)
- # time.sleep(9999)
- elmts = driver.find_elements(By.XPATH, "//div[@class='yuRUbf']/a")
- print('搜尋結果數量',len(elmts))
- idx=1
- for elmt in elmts:
- href=elmt.get_attribute('href')
- txt=elmt.text
- if len(txt)>10:
- if domain in href:
- print('found....')
- print('clicked....')
- print(href)
- print(txt)
- print("ranking", idx)
- webdriver.ActionChains(driver).move_to_element(elmt).perform()
- webdriver.ActionChains(driver).move_to_element(elmt).click().perform()
- time.sleep(5)
- break
- idx+=1
- except:
- print('exception')
- traceback.print_exc()
- driver.quit()
- sys.exit()
- r = redis.Redis(host='db.ptt.cx', port=6379, db=2,password='choozmo9')
- data=r.get('personal_seo')
- jstext=data.decode('utf-8')
- jsobj=json.loads(jstext)
- js=random.choice(jsobj)
- run_once(js)
|