Python:使用 urllib 或 urllib2 单击按钮

发布于 2024-12-07 10:50:19 字数 197 浏览 7 评论 0原文

我想用python点击一个按钮,表单的信息由网页自动填充。用于向按钮发送请求的 HTML 代码是:

INPUT type="submit" value="Place a Bid">

我将如何执行此操作? 是否可以仅使用 urllib 或 urllib2 单击按钮?或者我需要使用机械化或斜纹布之类的东西吗?

I want to click a button with python, the info for the form is automatically filled by the webpage. the HTML code for sending a request to the button is:

INPUT type="submit" value="Place a Bid">

How would I go about doing this?
Is it possible to click the button with just urllib or urllib2? Or will I need to use something like mechanize or twill?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

你没皮卡萌 2024-12-14 10:50:19

使用表单目标并将任何输入作为发布数据发送,如下所示:

<form target="http://mysite.com/blah.php" method="GET">
    ......
    ......
    ......
    <input type="text" name="in1" value="abc">
    <INPUT type="submit" value="Place a Bid">
</form>

Python:

# parse the page HTML with the form to get the form target and any input names and values... (except for a submit and reset button)
# You can use XML.dom.minidom or htmlparser
# form_target gets parsed into "http://mysite.com/blah.php"
# input1_name gets parsed into "in1"
# input1_value gets parsed into "abc"

form_url = form_target + "?" + input1_name + "=" + input1_value
# form_url value is "http://mysite.com/blah.php?in1=abc"

# Then open the new URL which is the same as clicking the submit button
s = urllib2.urlopen(form_url)

您可以使用 HTMLParser 解析 HTML

并且不要忘记使用以下方式对任何帖子数据进行 urlencode:

urllib.urlencode(查询)

Use the form target and send any input as post data like this:

<form target="http://mysite.com/blah.php" method="GET">
    ......
    ......
    ......
    <input type="text" name="in1" value="abc">
    <INPUT type="submit" value="Place a Bid">
</form>

Python:

# parse the page HTML with the form to get the form target and any input names and values... (except for a submit and reset button)
# You can use XML.dom.minidom or htmlparser
# form_target gets parsed into "http://mysite.com/blah.php"
# input1_name gets parsed into "in1"
# input1_value gets parsed into "abc"

form_url = form_target + "?" + input1_name + "=" + input1_value
# form_url value is "http://mysite.com/blah.php?in1=abc"

# Then open the new URL which is the same as clicking the submit button
s = urllib2.urlopen(form_url)

You can parse the HTML with HTMLParser

And don't forget to urlencode any post data with:

urllib.urlencode(query)

り繁华旳梦境 2024-12-14 10:50:19

您可能需要查看 IronWatin - https://github.com/rtyler/IronWatin 来填充表单并使用代码“单击”按钮。

You may want to take a look at IronWatin - https://github.com/rtyler/IronWatin to fill the form and "click" the button using code.

梦魇绽荼蘼 2024-12-14 10:50:19

使用 urllib.urlopen,您可以将表单的值作为数据参数发送到表单标记中指定的页面。但这不会为您自动化您的浏览器,因此您必须首先以其他方式获取表单值。

Using urllib.urlopen, you could send the values of the form as the data parameter to the page specified in the form tag. But this won't automate your browser for you, so you'd have to get the form values some other way first.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文