lxml.html的value属性 [英] value attribute for lxml.html

查看:105
本文介绍了lxml.html的value属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的代码:

from lxml.html import fromstring
#code
print fromstring(s).xpath('/html/body/div[3]/div/div[2]/div/form/input[4]')

输出为[<InputElement 2946d20 name='question' type='hidden'>]

如何输出值?有什么属性吗? 谢谢.

How can I output the value? Any attribute for this? Thank you.

推荐答案

通常使用 lxml ,您可以访问元素的直接通过.value属性设置值:

In general with lxml you can access an element's value directly via the .value attribute:

>>> from lxml.html import fromstring
>>> s = """<input type="hidden" name="question" value="1234">"""
>>> doc = fromstring(s)
>>> doc.value
'1234'

对于您来说,您还需要从 XPath 查询:

In your case you'll also need to access the first element of the resulting list from your XPath query:

print fromstring(s).xpath('/html/body/div[3]/div/div[2]/div/form/input[4]')[0].value

这篇关于lxml.html的value属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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