python使用win32com在百度空间插入html元素示例

yipeiwu_com6年前Python基础

复制代码 代码如下:

from win32com.client import DispatchEx
import time
ie=DispatchEx("InternetExplorer.Application")

ie.Navigate("http://hi.baidu.com/mirguest/creat/blog/")
ie.Visible=1
while ie.Busy:
    time.sleep(1)

body=ie.Document.body
# header
for i in body.getElementsByTagName("input"):
    if str(i.getAttribute("id"))=="spBlogTitle":
        print "Find title"
        i.value="AutoCreatedByPython"
        break

# editor
for i in body.getElementsByTagName("iframe"):
    print "Find iframe"
    if str(i.getAttribute("id"))=="tangram_editor_iframe_TANGRAM__1":
        print "Find"
        break
iframe=i
iframe.click()
sondoc=iframe.contentWindow.Document;
print sondoc
sonbody=sondoc.body
print sonbody
for ii in sonbody.getElementsByTagName("p"):
    print "Find p"
    ii.innerHTML="hello,my first try"
tmp=sondoc.createElement("div")
tmp.innerHTML="bye"
sonbody.insertBefore(tmp,ii)

tmpHTML="<div>hello 2</div>"
sonbody.insertAdjacentHTML("beforeEnd",tmpHTML)
'''
editor.getContentHTML
'''

# submit
for i in body.getElementsByTagName("div"):
    if str(i.getAttribute("id"))=="btn-box":
        print "Find button"
        break

btnbox=i
j=btnbox.childNodes(0)
j.click()

相关文章

Django用户认证系统 组与权限解析

Django的权限系统很简单,它可以赋予users或groups中的users以权限。 Django admin后台就使用了该权限系统,不过也可以用到你自己的代码中。 User对象具有两...

Python3写入文件常用方法实例分析

本文实例讲述了Python3写入文件常用方法。分享给大家供大家参考。具体如下: ''''' Created on Dec 18, 2012 写入文件 @author: liur...

python实现比对美团接口返回数据和本地mongo数据是否一致示例

本文实例讲述了python实现比对美团接口返回数据和本地mongo数据是否一致。分享给大家供大家参考,具体如下: 应用背景:美团平台商品的上下架状态、库存、售价,和mongo库存储的是否...

在Python的Flask框架下收发电子邮件的教程

 简述 在大多数此类教程中都会不遗余力的介绍如何使用数据库。今天我们对数据库暂且不表,而是来关注另一个在web应用中很重要的特性:如何推送邮件给用户。 在某个轻量级应用中我们可...

python图片验证码生成代码

本文实例为大家分享了python图片验证码实现代码,供大家参考,具体内容如下 #!/usr/bin/env python # -*- coding: UTF-8 -*- impo...