试图连接xpath descendant :: text items

时间:2016-10-18 20:31:31

标签: python xml xpath web-scraping

我试图从维基百科中抓取数据,特别是从以下页面

https://en.wikipedia.org/wiki/Los_Angeles_County_Sheriff%27s_Department

我想要在右侧的代理商概览vcard下获取标题和数据,但我在表格数据方面遇到了问题。一些/ td项包含子元素,这些子元素被记录为python列表中的另一个项目。

例如,这个XPath:

self.tree.xpath('//table[@class="infobox vcard"]/tr/td/descendant::text()[(preceding::*[contains(text(),"Agency overview")]) and (following::*[contains(text(),"Jurisdictional structure")])  ]')

返回:

['1850', 'Budgeted items:20,159 (2015)', '[1]', 'US$2,994,887,000', ' (2015)', '[1]', 'Governmental: ', 'Government agency']

由于有四个标题元素,我需要它返回四个数据元素。如何连接每个/ td下的所有元素?

0 个答案:

没有答案