如何在xpath python中转换一个变量 [英] how to cast a variable in xpath python

查看:23
本文介绍了如何在xpath python中转换一个变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

from lxml import html
import requests

pagina = 'http://www.beleggen.nl/amx'
page = requests.get(pagina)
tree = html.fromstring(page.text)

aandeel = tree.xpath('//a[@title="Imtech"]/text()')
print aandeel

这部分有效,但我想阅读具有不同标题的多行,是否可以将Imtech"部分更改为变量?

This part works, but I want to read multiple lines with different titles, is it possible to change the "Imtech" part to a variable?

这样的事情,显然不行,但我哪里出错了?还是没那么容易?

Something like this, it obviously doesnt work, but where did I go wrong? Or is it not quite this easy?

FondsName = "Imtech"
aandeel = tree.xpath('//a[@title="%s"]/text()')%(FondsName)
print aandeel

推荐答案

你几乎是对的:

variabelen = [var1,var2,var3]
for var in variabelen:
    aandeel = tree.xpath('//a[@title="%s"]/text()' % var)

这篇关于如何在xpath python中转换一个变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆