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

代码如下:

from win32com.client import dispatcheximport timeie=dispatchex(“internetexplorer.application”)

ie.navigate(“http://hi.baidu.com/mirguest/creat/blog/”)ie.visible=1while ie.busy: time.sleep(1)

body=ie.document.body# headerfor i in body.getelementsbytagname(“input”): if str(i.getattribute(“id”))==”spblogtitle”: print “find title” i.value=”autocreatedbypython” break

# editorfor i in body.getelementsbytagname(“iframe”): print “find iframe” if str(i.getattribute(“id”))==”tangram_editor_iframe_tangram__1″: print “find” breakiframe=iiframe.click()sondoc=iframe.contentwindow.document;print sondocsonbody=sondoc.bodyprint sonbodyfor ii in sonbody.getelementsbytagname(“p”): print “find p” ii.innerhtml=”hello,my first try”tmp=sondoc.createelement(“p”)tmp.innerhtml=”bye”sonbody.insertbefore(tmp,ii)

tmphtml=”

hello 2

“sonbody.insertadjacenthtml(“beforeend”,tmphtml)”’editor.getcontenthtml”’

# submitfor i in body.getelementsbytagname(“p”): if str(i.getattribute(“id”))==”btn-box”: print “find button” break

btnbox=ij=btnbox.childnodes(0)j.click()