# -*- coding: utf-8 -*-
import win32com.client
import time
ie = win32com.client.Dispatch('InternetExplorer.Application')
ie.Navigate('https://login.tudou.com/login.do?noreg=ok')
ie.Visible = 0 # 0=隱藏, 1=顯示
# 等待頁面加載完成
while ie.Busy:
time.sleep(1)
doc = ie.Document
doc.getElementById('email').value = 'your_account'
doc.getElementById('pass').value = 'your_password'
doc.getElementById('login_submit').click()
time.sleep(4) # 等待登錄跳轉
print('LOGIN SUCCESS')
# 後續可繼續操作 DOM、提取數據、截圖等
# ie.Quit() # 結束時關閉
Dispatch('InternetExplorer.Application') 啟動 IE 實例;Navigate(url) 導航,Busy 屬性輪詢等待加載;Document 返回 DOM 文檔對象,支持 getElementById、getElementsByTagName 等標準 DOM API;value 模擬輸入,click() 模擬點擊;ie.Document.cookie 獲取 Cookie 供 requests 會話複用。| 方案 | 優點 | 缺點 |
|---|---|---|
| IE COM 自動化 | 真實瀏覽器環境、完美執行 JS、處理複雜登錄流程 | 僅限 Windows、啟動慢、佔用資源大、IE 已停止維護 |
| Selenium + WebDriver | 跨瀏覽器、跨平台、生態成熟、支持無頭模式 | 需安裝驅動、較重 |
| requests + 手動構造表單 | 輕量、快、無依賴 | 無法執行 JS、難以應對動態 Token、驗證碼 |
| Playwright / Puppeteer (Python 綁定) | 現代、快、無頭、強大選擇器 | 需 Node 環境、學習曲線稍陡 |
新項目優先選 Playwright 或 Selenium;維舊項目或極簡腳本可用 IE COM。注意 IE 於 2022 年 6 月停止支持,長期專案請規劃遷移。