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

相关文章

django如何实现视图重定向

当请求访问到某个视图时,我们想让它重定向到其他页面,应该怎么做呢? 1.HttpResponseRedirect 需求:当我们访问127.0.0.1/my_redirect时跳到127...

以Flask为例讲解Python的框架的使用方法

以Flask为例讲解Python的框架的使用方法

了解了WSGI框架,我们发现:其实一个Web App,就是写一个WSGI的处理函数,针对每个HTTP请求进行响应。 但是如何处理HTTP请求不是问题,问题是如何处理100个不同的URL。...

Python实现图像的垂直投影示例

Python实现图像的垂直投影示例

Python + OpenCV 直接上代码 import cv2 import numpy as np from matplotlib import pyplot as plt...

python正则实现提取电话功能

本文实例为大家分享了python正则提取电话的具体代码,供大家参考,具体内容如下 主要用到正则 import re import xlrd def is_number(s):#是...

Python常用内置模块之xml模块(详解)

Python常用内置模块之xml模块(详解)

xml即可扩展标记语言,它可以用来标记数据、定义数据类型,是一种允许用户对自己的标记语言进行定义的源语言。从结构上,很像HTML超文本标记语言。但他们被设计的目的是不同的,超文本标记语言...