# -*- coding:UTF-8 -*-
import sys
from time import sleep
import win32com.client
from win32com.client import DispatchEx stdin, stdout, stderr = sys.stdin, sys.stdout, sys.stderr
reload(sys)
sys.setdefaultencoding("utf-8")
sys.stdin, sys.stdout, sys.stderr = stdin, stdout, stderr class COM_IE:
def __init__(self,url=None):
self.url = url
self.Visible = 1
self.ie = self.openIE(url)
self.document = ""
self.text = ""
self.charset = None def ExistIE(self,url):
ShellWindowsCLSID = '{9BA05972-F6A8-11CF-A442-00A0C90A8F39}'
ies=DispatchEx(ShellWindowsCLSID)
if len(ies)==0:
return None
for ie in ies:
if ie.LocationURL==url:
return ie
return None def NewIE(self,url):
ie = DispatchEx("InternetExplorer.Application")
ie.Visible = self.Visible
ie.Navigate(url)
return ie def openIE(self,url):
ie = self.ExistIE(url)
if ie==None:
ie = self.NewIE(url)
return ie def WaitIE(self):
# while self.ie.Busy:
# leep(1)
while 1:
state = self.ie.ReadyState
if state ==4:
# print "load done..."
self.charset = self.ie.Document.charset
self.document = self.ie.Document.body.innerHTML
self.text = self.ie.Document.body.innerText
break
sleep(1) def Visible(self):
self.ie.Visible = self.Visible def GetBody(self):
self.WaitIE()
return self.ie.Document.body def GetNodes(self,parentNode,tag):
"""
>>> coldiv=GetNodes(body,"div")
"""
childNodes=[]
for childNode in parentNode.getElementsByTagName(tag):
childNodes.append(childNode)
return childNodes def NodeByAttr(self,Nodes,nodeattr,nodeval):
"""
>>> div_id_editor=NodeByAttr(coldiv,"id","editor_ifr")
"""
for node in Nodes:
if str(node.getAttribute(nodeattr))==nodeval:
return node
return None def SetNodeHtml(self,body,node_type,node_attr,node_attr_val,node_inner_html):
tags = self.GetNodes(body,node_type)
node = self.NodeByAttr(tags,node_attr,node_attr_val)
node.innerHTML = node_inner_html def SetNodeVal(self,body,node_type,node_attr,node_attr_val,node_value):
tags = self.GetNodes(body,node_type)
node = self.NodeByAttr(tags,node_attr,node_attr_val)
node.value = node_value def NodeClick(self,body,node_type,node_attr,node_attr_val):
tags = self.GetNodes(body,node_type)
node = self.NodeByAttr(tags,node_attr,node_attr_val)
node.click() def GetNodeHtml(self,body,node_type,node_attr,node_attr_val):
tags = self.GetNodes(body,node_type)
node = self.NodeByAttr(tags,node_attr,node_attr_val)
html = node.innerHTML
return html def GetNodeVal(self,body,node_type,node_attr,node_attr_val):
tags = self.GetNodes(body,node_type)
node = self.NodeByAttr(tags,node_attr,node_attr_val)
value = node.value
return value #mutiple nodes
def NodesByAttr(self,Nodes,nodeattr=None,nodeval=None):
"""
>>> div_id_editor=NodeByAttr(coldiv,"id","editor_ifr")
"""
value_list = []
for node in Nodes:
# print node.nodeType,node.nodeName #,node.getAttribute("id"),node.innerText
value_dict = {}
if not nodeattr:
nodeattr_list = ["id","nodeName","nodeType","nodeValue","className",
"innerHTML","innerText","href","name","title","type","value"]
for attr in nodeattr_list:
value_dict[attr] = node.getAttribute(attr)
value_list.append(value_dict)
else:
if not nodeval:
value_dict[nodeattr] = node.getAttribute(nodeattr)
value_list.append(value_dict)
else:
if str(node.getAttribute(nodeattr))==nodeval:
value_dict[nodeattr] = node.getAttribute(nodeattr)
value_list.append(value_dict)
return value_list #mutiple nodes
def GetNodesVal(self,body,node_type,node_attr=None,node_val=None): # print '*'*50
tags = self.GetNodes(body,node_type)
value_list = self.NodesByAttr(tags,node_attr,node_val)
return value_list def Quit(self):
self.ie.Quit() if __name__=="__main__": url = "http://blog.csdn.net/agoago_2009/"
IE = COM_IE(url)
BODY = IE.GetBody() # a_list = IE.GetNodesVal(BODY,"a","href")
a_list = IE.GetNodesVal(BODY,"a")
for a in a_list:
print a.get("innerText"),a.get("href") '''
IE.SetNodeVal(BODY,"input","id","inputSearch","COM")
IE.NodeClick(BODY,"input","id","btnSubmit") IE.WaitIE()
print IE.document.strip()[:100]
print IE.charset
print IE.text.strip()[:100]
''' raw_input('quit')
IE.Quit()

随机推荐

  1. ASP.NET MVC中URL末尾斜杠的实现

    在网站的SEO优化中,通常都会涉及到URL结尾斜杠的问题. http://blog.sina.com.cn/s/blog_828e7ce40100srj1.html http://www.dengyo ...

  2. 用笔记本写C程序

    首先在Visual Studio Tools 文件中打开 Visual Studio 命令提示(2010),输入命令 notepad 打开记事本,写好程序保存,保存类型:所有文档  文件名:1.c . ...

  3. 《项目架构那点儿事》——Hibernate泛型Dao,让持久层简洁起来

    [前言]hibernate作为持久层ORM技术,它对JDBC进行非常轻量级对象封装,使得我们可以随心所欲的使用面向对象的思想来操作数据 库.同时,作为后台开发的支撑,的确扮演了一个举足轻重的角色,那么 ...

  4. 【解决方案】纯js动态克隆表一行元素

    1 m = 0 ;// 用于区分input // 新增一条录入 function AddTR(){ m += 1; var tableObject = document.getElementById( ...

  5. java 之 简单工厂模式(大话设计模式)

    以前只是看设计模式,每次看完都去理解一次,并没有手动去写代码,所以理解的还不是很深刻,最近查看框架源码,发现很多地方用到的都是设计模式,因为对设计模式理解的不够深刻,所以源码查看进度很慢!现在决定来温 ...

  6. 写一个PHP函数,实现扫描并打印出指定目录下(含子目录)的所有jpg文件名

    写一个PHP函数,实现扫描并打印出指定目录下(含子目录)的所有jpg文件名 <?php $dir = "E:\照片\\";//打印文件夹中所有jpg文件 function p ...

  7. 为什么ERP行业发展缓慢规模难扩大?

    题记:这个行业只会越来越好,但是能沉淀下来做事儿的企业越来越少,因为收益真的很漫长:能够真正进入这个行业难,出去也难... 经常在知乎上看到类似的问题:既然所有ERP系统都很烂,那有没有创业公司的空间 ...

  8. lumen 中间件详解

    我来给大家,讲解一下lumen中的中间件,高手勿喷. 首先,我们看下lumen中文档中的写法,我这里看的是5.3中文文档.https://lumen.laravel-china.org/docs/5. ...

  9. Qt颜色下拉框

    上周为了用Qt写一个类似颜色下拉框的东西,查阅了网上的多数相关资料,依然没有我想要的.终于在周四的时候下定决心重写QCombobox类来实现功能,现在把它贴出来,望看到的人,批评指正.废话不多说,先上 ...

  10. sklearn.neighbors.kneighbors_graph的简单属性介绍

    connectivity = kneighbors_graph(data, n_neighbors=7, mode='distance', metric='minkowski', p=2, inclu ...