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

查看:132
本文介绍了如何在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天全站免登陆