官术网_书友最值得收藏!

How to do it...

Now let's start playing with XPath and CSS selectors.  The following selects all <tr> elements with a class equal to "planet": 

In [2]: [(v, v.xpath("@name")) for v in tree.cssselect('tr.planet')]
Out[2]:
[(<Element tr at 0x10d3a2278>, ['Mercury']),
(<Element tr at 0x10c16ed18>, ['Venus']),
(<Element tr at 0x10e445688>, ['Earth']),
(<Element tr at 0x10e477228>, ['Mars']),
(<Element tr at 0x10e477408>, ['Jupiter']),
(<Element tr at 0x10e477458>, ['Saturn']),
(<Element tr at 0x10e4774a8>, ['Uranus']),
(<Element tr at 0x10e4774f8>, ['Neptune']),
(<Element tr at 0x10e477548>, ['Pluto'])]

Data for the Earth can be found in several ways. The following gets the row based on id:

In [3]: tr = tree.cssselect("tr#planet3")
...: tr[0], tr[0].xpath("./td[2]/text()")[0].strip()
...:
Out[3]: (<Element tr at 0x10e445688>, 'Earth')

The following uses an attribute with a specific value:

In [4]: tr = tree.cssselect("tr[name='Pluto']")
...: tr[0], tr[0].xpath("td[2]/text()")[0].strip()
...:
Out[5]: (<Element tr at 0x10e477548>, 'Pluto')

Note that unlike XPath, the @ symbol need not be used to specify an attribute.

主站蜘蛛池模板: 丰城市| 陆河县| 苗栗市| 凉山| 怀仁县| 合江县| 桑日县| 小金县| 肇东市| 韶关市| 阜新| 湘乡市| 云霄县| 旌德县| 上思县| 竹北市| 瑞昌市| 巴南区| 岑溪市| 扶沟县| 文山县| 武定县| 桃园市| 丹寨县| 徐汇区| 浏阳市| 镇平县| 南昌市| 平原县| 错那县| 贞丰县| 行唐县| 连城县| 平昌县| 苏尼特右旗| 克拉玛依市| 张家川| 靖州| 富民县| 依安县| 佛坪县|