Python:使用urllib或urllib2单击按钮

编程入门 行业动态 更新时间:2024-10-26 18:27:17
本文介绍了Python:使用urllib或urllib2单击按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

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

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">

我将如何去做? 是否可以仅使用urllib或urllib2单击按钮?还是我需要使用机械化或斜纹呢?

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?

推荐答案

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

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

<form target="mysite/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 "mysite/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 "mysite/blah.php?in1=abc" # Then open the new URL which is the same as clicking the submit button s = urllib2.urlopen(form_url)

您可以使用 HTMLParser

别忘了用以下代码对任何帖子数据进行urlencode:

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

urllib.urlencode(query)

更多推荐

Python:使用urllib或urllib2单击按钮

本文发布于:2023-10-09 16:48:33,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1476239.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:单击   按钮   Python   urllib

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!