Xpath结合了两个树路径

时间:2016-08-11 20:00:32

标签: python xpath

我有两个电话:

teamName = tree.xpath('//span[@class="name matchwinner"]/text()')
teamMatch = tree.xpath('//a[@class="match"]/@href')

目前我得到两个阵列,一个是团队名,另一个是href。有没有办法得到像[team1,href1,team2,href2]这样的东西?

1 个答案:

答案 0 :(得分:0)

我怀疑每个球队的名字和球队比赛共享同一个父母。如果是这种情况,您可以执行以下操作:

for team in tree.xpath("//div[@class='game']"):
    teamName = team.xpath('.//span[@class="name matchwinner"]/text()')[0]
    teamMatch = team.xpath('.//a[@class="match"]/@href')[0]

    print(teamName, teamMatch)

或者,您也可以使用zip()按名称对名称和匹配进行分组:

teamName = tree.xpath('//span[@class="name matchwinner"]/text()')
teamMatch = tree.xpath('//a[@class="match"]/@href')

print(list(zip(teamName, teamMatch)))