Python - 表单的xpath语法(lxml.html)

0 投票
1 回答
610 浏览
提问于 2025-04-18 08:45
<form method="post" name="login_form" action="/submit">

我正在尝试获取一个叫做 action 的属性。

我试过这个方法:

print fromstring(source).xpath('.//form[@action]')[0].text,但是它输出的是

None

1 个回答

1

你的XPath表达式返回的是<form>元素,而不是属性。

要从这个元素中获取属性:

print fromstring(source).xpath('.//form[@action]')[0].get('action')

撰写回答