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通过线程实现定时器timer的方法

本文实例讲述了python通过线程实现定时器timer的方法。分享给大家供大家参考。具体分析如下: 这个python类实现了一个定时器效果,调用非常简单,可以让系统定时执行指定的函数 下...

Python数据结构之哈夫曼树定义与使用方法示例

Python数据结构之哈夫曼树定义与使用方法示例

本文实例讲述了Python数据结构之哈夫曼树定义与使用方法。分享给大家供大家参考,具体如下: HaffMan.py #coding=utf-8 #考虑权值的haff曼树查找效率并非最...

在Python中使用正则表达式的方法

正则表达式(regular expression)是一种用形式化语法描述的文本匹配模式。在需要处理大量文本处理的应用中有广泛的使用,我没使用的编辑器,IDE中的搜索常用正则表达式作为搜索...

Flask-Mail用法实例分析

Flask-Mail用法实例分析

本文实例讲述了Flask-Mail用法。分享给大家供大家参考,具体如下: 很多类型的应用程序都需要在特定事件发生时提醒用户,而常用的通信方法是电子邮件。 虽然 Python 标准库中的...

浅谈python中的占位符

占位符,顾名思义就是插在输出里站位的符号。我们可以把它理解成我们预定饭店。当我们告诉饭店的时候,饭店的系统里会有我们的预定位置。虽然我们现在没有去但是后来的顾客就排在我们后面。 常见的占...