获取 HTML 表单输入字段的值?

Grabbing the value of a HTML form input field?

有一个网页类似于:www.example.com/form.php

我想使用 Python 从页面上的 HTML 表单中获取其中一个值。例如,如果表单有我可以得到值 "test" returned

我已经用谷歌搜索了很多,但大多数都与发布表单数据有关,或者有使用 Django 或 cgi-bin 的建议。我无权直接访问服务器,所以我不能那样做。

我认为库 REQUESTS 可以做到,但我在文档中看不到它。

HTML:

<html>
<body>
<form method="" action="formpost.php" name="form1" id="form1">
<input type="text" name"field1" value="this is field1">
<input type="hidden" name="key" value="secret key field">
</form>
</body>

例如,我想要 Python 中的这样的内容:

import special_library

html = special_library.get("http://www.example.com/form.php")
print html.get_field("wanted")

有没有人有任何建议来实现这一目标?或者我可能没有想到或不知道的任何库?

您可以使用 requests 库,并且 lxml

试试这个:

import requests
from lxml import html

s = requests.Session()
resp = s.get("http://www.example.com/form.php")
doc = html.fromstring(resp.text)

wanted_value = doc.xpath("//input[@class='wanted_class_name']/@value")
print(wanted_value)

您可以查看以下资源: