本文实例讲述了Python使用win32com实现的模拟浏览器功能。分享给大家供大家参考,具体如下:
# -*- 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程序设计有所帮助。
新闻热点
疑难解答