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

Python-表单的xpath语法(lxml.html),python,lxml.html,Python,Lxml.html,我正在尝试获取动作属性 我试过了 print fromstring(source).xpath('.//form[@action]')[0]。text,但它会打印 None您的XPath表达式返回的是元素,而不是属性 从元素中获取属性: <form method="post" name="login_form" action="/submit"> print fromstring(source).xpath('.//form[@action]')[0].get('action')

我正在尝试获取动作属性 我试过了

print fromstring(source).xpath('.//form[@action]')[0]。text
,但它会打印


None

您的XPath表达式返回的是
元素,而不是属性

从元素中获取属性:

<form method="post" name="login_form" action="/submit">
print fromstring(source).xpath('.//form[@action]')[0].get('action')