本文實例講述了Python使用win32com實現(xiàn)的模擬瀏覽器功能。分享給大家供大家參考,具體如下:
# -*- coding:UTF-8 -*-#!/user/bin/env python'''Created on 2010-9-1@author: chenzehe'''import win32com.clientfrom time import sleeploginurl='http://passport.cnblogs.com/login.aspx'loginouturl='http://passport.cnblogs.com/logout.aspx'username='XXX'password='XXX'ie = win32com.client.Dispatch("InternetExplorer.Application")ie.Visible = 0ie.Navigate(loginurl)state = ie.ReadyStateprint "打開登陸頁面"while 1: state = ie.ReadyState if state ==4: break sleep(1)print "頁面載入完畢,輸入用戶名密碼"state = Noneie.Document.getElementById("tbUserName").value=usernameie.Document.getElementById("tbPassword").value=passwordie.Document.getElementById("btnLogin").click()while 1: state = ie.ReadyState print state if state ==4 and str(ie.LocationURL) == "http://home.cnblogs.com/": break sleep(1)print "登陸成功"print '你的昵稱是:'print ie.Document.getElementById('lnk_current_user').title#博客園只能登錄一次,注銷print '注銷!'ie.Navigate(loginouturl)
希望本文所述對大家Python程序設計有所幫助。
新聞熱點
疑難解答
圖片精選