python使用win32com在百度空间插入html元素示例
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()
上一篇: 下载给定网页上图片的方法