欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2023-08-14 09:17:19
复制代码 代码如下:from win32com.client import dispatcheximport timeie=dispatchex("internetexpl...

复制代码 代码如下:

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