使用pythonlxm提交表单

2024-09-29 17:24:11 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个这样的登录页面

<form method="POST" name="DefaultForm" action="SOME_URL" onSubmit="return (isReady(this));" autocomplete="off">

<input name="action" type="hidden" value="SOME_VALUE">
<input name="serverTimeStamp" type="hidden" value="SOME_VALUE">
<input name="clientTimeStamp" type="hidden" value="">
<input name="clientIP" type="hidden" value="SOME_VALUE">

<TABLE height="400" cellSpacing="0" cellPadding="0" width="540" align="center" background="images/bkground.gif" border="0">
...
<INPUT class="inputStyle" type="Input"  name="username" size="20">
<INPUT class="inputStyle" type="password"  maxLength="28" name="password" size="20">
...
</TABLE>
</form>

使用Python和lxml/Requests模块

^{pr2}$

但是我不能让它工作,find()总是返回None总是到else块。有人能提出解决办法吗?在


Tags: nameforminputsizevaluetypetableaction
1条回答
网友
1楼 · 发布于 2024-09-29 17:24:11

为什么看不到是因为表单是元素:

    h = """<form method="POST" name="DefaultForm" action="SOME_URL" onSubmit="return (isReady(this));" autocomplete="off">

<input name="action" type="hidden" value="SOME_VALUE">
<input name="serverTimeStamp" type="hidden" value="SOME_VALUE">
<input name="clientTimeStamp" type="hidden" value="">
<input name="clientIP" type="hidden" value="SOME_VALUE">

<TABLE height="400" cellSpacing="0" cellPadding="0" width="540" align="center" background="images/bkground.gif" border="0">
<INPUT class="inputStyle" type="Input"  name="username" size="20">
<INPUT class="inputStyle" type="password"  maxLength="28" name="password" size="20">
</TABLE>
</form>"""

x = html.fromstring(h)
print(x.attrib)
print(x)
print(x.find("form"))

只需执行上述操作:

^{pr2}$

如果我们用div来包装表单:

h = """<div>
      <form method="POST" name="DefaultForm" action="SOME_URL" onSubmit="return (isReady(this));" autocomplete="off">
<input name="action" type="hidden" value="SOME_VALUE">
<input name="serverTimeStamp" type="hidden" value="SOME_VALUE">
<input name="clientTimeStamp" type="hidden" value="">
<input name="clientIP" type="hidden" value="SOME_VALUE">
<TABLE height="400" cellSpacing="0" cellPadding="0" width="540" align="center" background="images/bkground.gif" border="0">
<INPUT class="inputStyle" type="Input"  name="username" size="20">
<INPUT class="inputStyle" type="password"  maxLength="28" name="password" size="20">
</TABLE>
</form>
</div>"""

from lxml import html
import lxml.etree as et
x = html.fromstring(h)
print x
print(x.find("form"))

现在div是根,find找到以下形式:

<Element div at 0x7f05966b3b50>
<Element form at 0x7f0597a44ba8>

相关问题 更多 >

    热门问题