gpt4 book ai didi

python - 第二次出现的 xPath 语法

转载 作者:太空宇宙 更新时间:2023-11-03 18:31:07 24 4
gpt4 key购买 nike

<input name="utf8" type="hidden" value="&#x2713;" />
<input name="ohboy" type="hidden" value="I_WANT_THIS" />
<label for="user_email">Email</label>
<input class="form-control" id="user_email" name="user[email]" size="30" type="email" value="" />

我有点卡在这里,我最初打算使用 find() 而不是 xpath() 因为标签输入位于源中的多个位置,但我发现 find() 只返回第一次出现来源

最佳答案

使用 find(),传递指定元素的整数索引的 xpath 表达式:

from lxml.html import fromstring


html_data = """<input name="utf8" type="hidden" value="&#x2713;" />
<input name="ohboy" type="hidden" value="I_WANT_THIS" />
<label for="user_email">Email</label>
<input class="form-control" id="user_email" name="user[email]" size="30" type="email" value="" />"""

tree = fromstring(html_data)
print tree.find('.//input[2]').attrib['value']

打印:

I_WANT_THIS

但是,更好(也更干净)的是通过名称属性查找输入:

print tree.find('.//input[@name="ohboy"]').attrib['value']

关于python - 第二次出现的 xPath 语法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22367914/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com