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

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.

主站蜘蛛池模板: 五寨县| 元阳县| 芜湖市| 海兴县| 清苑县| 泊头市| 佛坪县| 洱源县| 黄石市| 闽侯县| 视频| 两当县| 罗定市| 虹口区| 子洲县| 孟村| 梓潼县| 涟水县| 攀枝花市| 崇礼县| 南乐县| 富蕴县| 讷河市| 周宁县| 丘北县| 准格尔旗| 囊谦县| 霍林郭勒市| 江永县| 蒲城县| 东光县| 阜康市| 清苑县| 司法| 顺义区| 镇江市| 班戈县| 龙陵县| 边坝县| 东源县| 桐柏县|