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()

相关文章

python实现汉诺塔递归算法经典案例

python实现汉诺塔递归算法经典案例

    学到递归的时候有个汉诺塔的练习,汉诺塔应该是学习计算机递归算法的经典入门案例了,所以本人觉得可以写篇博客来表达一下自己的见解。这markdown编辑...

python写日志文件操作类与应用示例

本文实例讲述了python写日志文件操作类与应用。分享给大家供大家参考,具体如下: 项目的开发过程中,日志文件是少不了的,通过写日志文件,可以知道程序运行的情况。特别当部署在生产环境中的...

Python测试人员需要掌握的知识

a、字符串的定义方法 使用单引号(') 你可以用单引号指示字符串,就如同'Quote me on this'这样。所有的空白,即空格和制表符都照原样保留。 使用双引号(") 在双...

Python实现图像几何变换

本文实例讲述了Python实现图像几何变换的方法。分享给大家供大家参考。具体实现方法如下: import Image try: im=Image.open('test.jpg')...

Python3读取文件常用方法实例分析

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