# HG changeset patch # User Thinker K.F. Li # Date 1225591191 -28800 # Node ID 9f248c8460cea5c9f76a182008ad0e192b520090 # Parent 0cfc7a19a4d241325793766e17b0b738b1fc0964 Simplize xpath patterns. - Use 'descendant' aix name simplize patterns. diff -r 0cfc7a19a4d2 -r 9f248c8460ce pyikriam/buildings.py --- a/pyikriam/buildings.py Sun Nov 02 04:12:23 2008 +0800 +++ b/pyikriam/buildings.py Sun Nov 02 09:59:51 2008 +0800 @@ -20,7 +20,7 @@ class townhall(position): xpath_patterns = { - 'level': '/div/div/div[@class=\'buildingLevel\']/text()' + 'level': 'div[@class=\'buildingLevel\']/text()' } def __init__(self, city_id, idx, baseurl): @@ -30,7 +30,7 @@ def _sync(self, page): parser = etree.HTMLParser(encoding='utf8') page_dom = etree.parse(StringIO(page), parser) - xpath_building = '/html/body/div/div' + xpath_building = '/html/body/descendant::' for name, ptn in self.xpath_patterns.items(): path = xpath_building + ptn value = page_dom.xpath(path)[0] diff -r 0cfc7a19a4d2 -r 9f248c8460ce pyikriam/ikariam.py --- a/pyikriam/ikariam.py Sun Nov 02 04:12:23 2008 +0800 +++ b/pyikriam/ikariam.py Sun Nov 02 09:59:51 2008 +0800 @@ -70,13 +70,13 @@ class IkariamCity: data_patterns = { - 'gold': '/div[@id=\'globalResources\']/ul/li/a/span[@id=\'value_gold\']/text()', - 'inhabitants': '/div[@id=\'cityResources\']/ul/li/span[@id=\'value_inhabitants\']/text()', - 'wood': '/div[@id=\'cityResources\']/ul/li/span[@id=\'value_wood\']/text()', - 'wine': '/div[@id=\'cityResources\']/ul/li/span[@id=\'value_wine\']/text()', - 'marble': '/div[@id=\'cityResources\']/ul/li/span[@id=\'value_marble\']/text()', - 'crystal': '/div[@id=\'cityResources\']/ul/li/span[@id=\'value_crystal\']/text()', - 'sulfur': '/div[@id=\'cityResources\']/ul/li/span[@id=\'value_sulfur\']/text()' + 'gold': 'value_gold', + 'inhabitants': 'value_inhabitants', + 'wood': 'value_wood', + 'wine': 'value_wine', + 'marble': 'value_marble', + 'crystal': 'value_crystal', + 'sulfur': 'value_sulfur' } def __init__(self, id, core ): self.core = core @@ -88,14 +88,14 @@ parser = etree.HTMLParser(encoding='utf8') page_dom = etree.parse(StringIO(page), parser) - xpath_globalinfo = "/html/body[@id='city']/div[@id='container']/div[@id='container2']" - for name, path in self.data_patterns.items(): - xpath = xpath_globalinfo + path + xpath_globalinfo = '/descendant::*[@id=\'%s\']/text()' + for name, tag_id in self.data_patterns.items(): + xpath = xpath_globalinfo % (tag_id) value = page_dom.xpath(xpath)[0] setattr(self, name, value) pass - xpath_mainview = '/html/body/div/div/div[@id=\'mainview\']/ul/li' + xpath_mainview = '/descendant::div[@id=\'mainview\']/ul/li' pos_doms = page_dom.xpath(xpath_mainview) positions = [self._mk_position(pos_dom, idx) for idx, pos_dom in enumerate(pos_doms)]