1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980 |
- import time
- import traceback
- import dataset
- from selenium import webdriver
- from selenium.webdriver.support.ui import WebDriverWait
- from selenium.webdriver.common.by import By
- from selenium.webdriver.support import expected_conditions as EC
- from selenium.webdriver.common.keys import Keys
- #import undetected_chromedriver as uc
-
- # options = webdriver.ChromeOptions()
- # options.add_argument('--no-sandbox')
- # options.add_argument('start-maximized')
- # options.add_argument('enable-automation')
- # options.add_argument('--disable-infobars')
- # options.add_argument('--disable-dev-shm-usage')
- # options.add_argument('--disable-browser-side-navigation')
- # options.add_argument("--remote-debugging-port=9222")
- # # options.add_argument("--headless")
- # options.add_argument('--disable-gpu')
- # options.add_argument("--log-level=3")
- # driver = webdriver.Chrome(ChromeDriverManager().install(), chrome_options=options)
- options = webdriver.ChromeOptions()
- options.add_argument("--no-sandbox")
- options.add_argument("--headless")
- options.add_argument("--incognito")
- #options.add_argument('--blink-settings=imagesEnabled=false')
- mobile_emulation = {
- "deviceMetrics": { "width": 360, "height": 640, "pixelRatio": 3.0 },
- "userAgent": "Mozilla/5.0 (Linux; Android 4.2.1; en-us; Nexus 5 Build/JOP40D) AppleWebKit/535.19 (KHTML, like Gecko) Chrome/18.0.1025.166 Mobile Safari/535.19" }
- try:
- driver = webdriver.Chrome(options=options)
- # driver = webdriver.Chrome(ChromeDriverManager().install(),options=options)
- ## Create Undetected Chromedriver with Options
- # driver = uc.Chrome(options=options)
- # options = uc.ChromeOptions()
- except:
- traceback.print_exc()
- #kw='真理大學國際生'
- #domain='cia.au.edu.tw'
- kw='真理大學校友'
- domain='au.edu.tw'
- driver.get('https://www.google.com?num=100')
- time.sleep(3)
- print(driver.current_url)
- elmt = driver.find_element(By.XPATH, "//textarea[@name='q']")
- time.sleep(1)
- elmt.send_keys(kw)
- elmt.send_keys(Keys.ENTER)
- time.sleep(4)
- elmts = driver.find_elements(By.XPATH, "//a[@jsname='UWckNb']")
- numresults=len(elmts)
- print('搜尋結果數量',numresults)
- if numresults<=0:
- print(elmts)
- for elmt in elmts:
- href=elmt.get_attribute('href')
- txt=elmt.text
- # print(txt)
- # print(href)
- if domain in href:
- webdriver.ActionChains(driver).move_to_element(elmt).perform()
- webdriver.ActionChains(driver).move_to_element(elmt).click().perform()
- print(domain)
- print(href)
- break
- time.sleep(5)
|