zl程序教程

您现在的位置是:首页 >  后端

当前栏目

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

Python百度HTML 使用 示例 空间 元素 插入
2023-06-13 09:15:18 时间

复制代码代码如下:


fromwin32com.clientimportDispatchEx
importtime
ie=DispatchEx("InternetExplorer.Application")

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

body=ie.Document.body
#header
foriinbody.getElementsByTagName("input"):
   ifstr(i.getAttribute("id"))=="spBlogTitle":
       print"Findtitle"
       i.value="AutoCreatedByPython"
       break

#editor
foriinbody.getElementsByTagName("iframe"):
   print"Findiframe"
   ifstr(i.getAttribute("id"))=="tangram_editor_iframe_TANGRAM__1":
       print"Find"
       break
iframe=i
iframe.click()
sondoc=iframe.contentWindow.Document;
printsondoc
sonbody=sondoc.body
printsonbody
foriiinsonbody.getElementsByTagName("p"):
   print"Findp"
   ii.innerHTML="hello,myfirsttry"
tmp=sondoc.createElement("div")
tmp.innerHTML="bye"
sonbody.insertBefore(tmp,ii)

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

#submit
foriinbody.getElementsByTagName("div"):
   ifstr(i.getAttribute("id"))=="btn-box":
       print"Findbutton"
       break

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