Mercurial > eagle-eye
annotate ikweb/tools/lazy/www/work/find.py @ 247:7747bbe5b68e
start to develope Information Exchange Center of Ikariam Game. (prototpye)
author | "Hisn Yi, Chen <ossug.hychen@gmail.com>" |
---|---|
date | Mon, 01 Dec 2008 00:27:22 +0800 |
parents | |
children |
rev | line source |
---|---|
247
7747bbe5b68e
start to develope Information Exchange Center of Ikariam Game. (prototpye)
"Hisn Yi, Chen <ossug.hychen@gmail.com>"
parents:
diff
changeset
|
1 from lxml import etree |
7747bbe5b68e
start to develope Information Exchange Center of Ikariam Game. (prototpye)
"Hisn Yi, Chen <ossug.hychen@gmail.com>"
parents:
diff
changeset
|
2 from cStringIO import StringIO |
7747bbe5b68e
start to develope Information Exchange Center of Ikariam Game. (prototpye)
"Hisn Yi, Chen <ossug.hychen@gmail.com>"
parents:
diff
changeset
|
3 |
7747bbe5b68e
start to develope Information Exchange Center of Ikariam Game. (prototpye)
"Hisn Yi, Chen <ossug.hychen@gmail.com>"
parents:
diff
changeset
|
4 class Finder: |
7747bbe5b68e
start to develope Information Exchange Center of Ikariam Game. (prototpye)
"Hisn Yi, Chen <ossug.hychen@gmail.com>"
parents:
diff
changeset
|
5 |
7747bbe5b68e
start to develope Information Exchange Center of Ikariam Game. (prototpye)
"Hisn Yi, Chen <ossug.hychen@gmail.com>"
parents:
diff
changeset
|
6 dom_tree = None |
7747bbe5b68e
start to develope Information Exchange Center of Ikariam Game. (prototpye)
"Hisn Yi, Chen <ossug.hychen@gmail.com>"
parents:
diff
changeset
|
7 xpath = None |
7747bbe5b68e
start to develope Information Exchange Center of Ikariam Game. (prototpye)
"Hisn Yi, Chen <ossug.hychen@gmail.com>"
parents:
diff
changeset
|
8 |
7747bbe5b68e
start to develope Information Exchange Center of Ikariam Game. (prototpye)
"Hisn Yi, Chen <ossug.hychen@gmail.com>"
parents:
diff
changeset
|
9 def __init__(self, working_product): |
7747bbe5b68e
start to develope Information Exchange Center of Ikariam Game. (prototpye)
"Hisn Yi, Chen <ossug.hychen@gmail.com>"
parents:
diff
changeset
|
10 self.working_prodcut = working_product |
7747bbe5b68e
start to develope Information Exchange Center of Ikariam Game. (prototpye)
"Hisn Yi, Chen <ossug.hychen@gmail.com>"
parents:
diff
changeset
|
11 |
7747bbe5b68e
start to develope Information Exchange Center of Ikariam Game. (prototpye)
"Hisn Yi, Chen <ossug.hychen@gmail.com>"
parents:
diff
changeset
|
12 self.encoding = 'utf8' |
7747bbe5b68e
start to develope Information Exchange Center of Ikariam Game. (prototpye)
"Hisn Yi, Chen <ossug.hychen@gmail.com>"
parents:
diff
changeset
|
13 |
7747bbe5b68e
start to develope Information Exchange Center of Ikariam Game. (prototpye)
"Hisn Yi, Chen <ossug.hychen@gmail.com>"
parents:
diff
changeset
|
14 def find(self, express , callback = None): |
7747bbe5b68e
start to develope Information Exchange Center of Ikariam Game. (prototpye)
"Hisn Yi, Chen <ossug.hychen@gmail.com>"
parents:
diff
changeset
|
15 |
7747bbe5b68e
start to develope Information Exchange Center of Ikariam Game. (prototpye)
"Hisn Yi, Chen <ossug.hychen@gmail.com>"
parents:
diff
changeset
|
16 if self.dom_tree is None: self.set_dom_tree(self.working_prodcut.content) |
7747bbe5b68e
start to develope Information Exchange Center of Ikariam Game. (prototpye)
"Hisn Yi, Chen <ossug.hychen@gmail.com>"
parents:
diff
changeset
|
17 |
7747bbe5b68e
start to develope Information Exchange Center of Ikariam Game. (prototpye)
"Hisn Yi, Chen <ossug.hychen@gmail.com>"
parents:
diff
changeset
|
18 xpath = self.dom_tree.xpath(express) |
7747bbe5b68e
start to develope Information Exchange Center of Ikariam Game. (prototpye)
"Hisn Yi, Chen <ossug.hychen@gmail.com>"
parents:
diff
changeset
|
19 |
7747bbe5b68e
start to develope Information Exchange Center of Ikariam Game. (prototpye)
"Hisn Yi, Chen <ossug.hychen@gmail.com>"
parents:
diff
changeset
|
20 if callback: return self.callback(xpath) |
7747bbe5b68e
start to develope Information Exchange Center of Ikariam Game. (prototpye)
"Hisn Yi, Chen <ossug.hychen@gmail.com>"
parents:
diff
changeset
|
21 return xpath |
7747bbe5b68e
start to develope Information Exchange Center of Ikariam Game. (prototpye)
"Hisn Yi, Chen <ossug.hychen@gmail.com>"
parents:
diff
changeset
|
22 |
7747bbe5b68e
start to develope Information Exchange Center of Ikariam Game. (prototpye)
"Hisn Yi, Chen <ossug.hychen@gmail.com>"
parents:
diff
changeset
|
23 def set_dom_tree(self, content): |
7747bbe5b68e
start to develope Information Exchange Center of Ikariam Game. (prototpye)
"Hisn Yi, Chen <ossug.hychen@gmail.com>"
parents:
diff
changeset
|
24 stream = StringIO(content) |
7747bbe5b68e
start to develope Information Exchange Center of Ikariam Game. (prototpye)
"Hisn Yi, Chen <ossug.hychen@gmail.com>"
parents:
diff
changeset
|
25 |
7747bbe5b68e
start to develope Information Exchange Center of Ikariam Game. (prototpye)
"Hisn Yi, Chen <ossug.hychen@gmail.com>"
parents:
diff
changeset
|
26 parser = etree.XMLParser(encoding=self.encoding) |
7747bbe5b68e
start to develope Information Exchange Center of Ikariam Game. (prototpye)
"Hisn Yi, Chen <ossug.hychen@gmail.com>"
parents:
diff
changeset
|
27 self.dom_tree = etree.parse(stream, parser) |