| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384 |
- import os
- import random
- import re
- import time
- from selenium import webdriver
- from selenium.webdriver.chrome.service import Service
- from selenium.webdriver.common.by import By
- from selenium.webdriver.common.keys import Keys
- from selenium.webdriver.common.proxy import Proxy
- from selenium.webdriver.support import expected_conditions as EC
- from selenium.webdriver.support.ui import WebDriverWait
- def startChrome():
-
- """启动Chrome浏览器,需要指定驱动的位置
- """
- chrome_driver = os.path.abspath(r"C:\Program Files (x86)\Google\Chrome\Application\chromedriver.exe")
- os.environ["webdriver.chrome.driver"] = chrome_driver
-
- webdriver.DesiredCapabilities.CHROME['proxy'] = {
- "httpProxy": "182.90.14.241:80",
- "sslProxy": "59.78.160.248:8080",
- "noProxy": None,
- "proxyType": "MANUAL",
- "autodetect": False
- }
-
- driver = webdriver.Chrome(chrome_driver)
- wait = WebDriverWait(driver, 10)
-
- # login
- driver.get('https://www.mouser.cn/MyMouser/MouserLogin.aspx')
- wait.until(EC.visibility_of_element_located((By.ID, 'ctl00_ContentMain_login_login_UserName')))
- wait.until(EC.visibility_of_element_located((By.ID, 'ctl00_ContentMain_login_login_Password')))
- wait.until(EC.element_to_be_clickable((By.ID, 'ctl00_ContentMain_login_login_LoginButton')))
- name_el = driver.find_element(By.ID, 'ctl00_ContentMain_login_login_UserName')
- name_el.send_keys('_mouser_')
- pwd_el = driver.find_element(By.ID, 'ctl00_ContentMain_login_login_Password')
- pwd_el.send_keys('select123')
- time.sleep(random.randint(1, 3))
- btn_el = driver.find_element(By.ID, 'ctl00_ContentMain_login_login_LoginButton')
- btn_el.click()
-
- wait.until(lambda d: d.current_url == 'https://www.mouser.cn/MyMouser/AccountSummary.aspx')
-
- # index
- wait.until(EC.element_to_be_clickable((By.ID, 'ctl00_Header2_RepeaterNav_ctl00_hyperHeaderNav')))
- time.sleep(random.randint(1, 3))
- btn_el = driver.find_element(By.ID, 'ctl00_Header2_RepeaterNav_ctl00_hyperHeaderNav')
- btn_el.click()
-
- wait.until(lambda d: d.current_url == 'http://www.mouser.cn/Electronic-Components/')
-
- # category home
- wait.until(EC.element_to_be_clickable((By.CLASS_NAME, 'SearchResultsSubLevelCategory')))
- time.sleep(random.randint(1, 3))
- btn_el = driver.find_element(By.CLASS_NAME, 'SearchResultsSubLevelCategory')
- btn_el.click()
-
- wait.until(lambda d: re.match('.+\/_\/.+', d.current_url))
-
- # list
- time.sleep(random.randint(1, 3))
- driver.refresh()
-
- cmp_el = driver.find_element(By.CSS_SELECTOR, ".mfrDiv a")
- time.sleep(random.randint(1, 3))
- cmp_el.click()
-
- wait.until(EC.element_to_be_clickable((By.ID, 'ctl00_ContentMain_btn1')))
-
- # detail
- print(driver.current_url)
- print(driver.page_source)
-
- driver.close()
- driver.quit()
- driver = None
-
- if __name__ == '__main__':
- startChrome()
|