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()

相关文章

使用Matplotlib 绘制精美的数学图形例子

使用Matplotlib 绘制精美的数学图形例子

一个最最简单的例子: 绘制一个从 0 到 360 度完整的 SIN 函数图形 import numpy as np import matplotlib.pyplot as pt...

python2.7使用plotly绘制本地散点图和折线图

python2.7使用plotly绘制本地散点图和折线图

本人在学习使用Python和plotly处理数据时,经过两个小时艰难试错,终于完成了散点图和折线图的实例。在使用过程中遇到一个大坑,因为官方给出的案例是用在线存储的,所以需要安装jupy...

Python中os和shutil模块实用方法集锦

复制代码 代码如下:# os 模块os.sep 可以取代操作系统特定的路径分隔符。windows下为 '\\'os.name 字符串指示你正在使用的平台。比如对于Wi...

Python字符串处理实例详解

Python字符串处理实例详解 一、拆分含有多种分隔符的字符串 1.如何拆分含有多种分隔符的字符串 问题: 我们要把某个字符串依据分隔符号拆分不同的字段,该字符串包含多种不同的分隔符,例...

flask 实现上传图片并缩放作为头像的例子

个人开发的 flask 论坛进入尾声,还剩最后一个上传图片更换头像功能,搞了一整天,最后终于解决了所有问题,现在记录下解决方案。 1. 上传文件 分析一下更换头像功能,我们需要做哪些事,...