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

yipeiwu_com5年前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最基本的输入输出详解

输出 用print加上字符串,就可以向屏幕上输出指定的文字。比如输出'hello, world',用代码实现如下: >>> print 'hello, world'...

pandas.dataframe中根据条件获取元素所在的位置方法(索引)

在dataframe中根据一定的条件,得到符合要求的某行元素所在的位置。 代码如下所示: df = pd.DataFrame({'BoolCol': [1, 2, 3, 3, 4],...

django 通过url实现简单的权限控制的例子

根据用户权限设定用户可以访问哪些页面,用django实现一个简单的demo。 1.models.py 文件 class level(models.Model): l_name =...

python基于TCP实现的文件下载器功能案例

本文实例讲述了python基于TCP实现的文件下载器功能。分享给大家供大家参考,具体如下: 服务器 参考代码如下: from socket import * import sys d...

Python3实现二叉树的最大深度

Python3实现二叉树的最大深度

问题提出: 给定一个二叉树,找出其最大深度。二叉树的深度为根节点到最远叶子节点的最长路径上的节点数。 说明: 叶子节点是指没有子节点的节点。 解决思路:递归法求解。从根结点向下遍历,...