在没有名称属性的Python中提交表单 [英] Form Submission in Python Without Name Attribute

查看:232
本文介绍了在没有名称属性的Python中提交表单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

背景:

使用Python中的urllib和urllib2,您可以进行表单提交。



您首先创建一本字典。

  formdictionary = {'search ':'stackoverflow'} 

然后使用urllib的urlencode方法来转换这个字典。

  params = urllib.urlencode(formdictionary)

您现在可以使用urllib2发出url请求,并将第一个参数作为url传递给变量params作为辅助参数。

  open = urllib2.urlopen('www.searchpage.com',params)



根据我的理解,urlencode自动将字典编码为html并添加输入标签。它的关键是成为名称属性。它在字典中的值是name属性的值。 Urlib2通过一个HTTP POST请求发送这个html代码。



问题:

这是正常的,如果html代码

 < input id =您输入的内容是以标准方式格式化的, 32324type =textname =search> 

但是,存在HTML代码格式不正确的情况。而html输入标签只有一个id属性no name属性。是否有另一种方法通过id属性访问输入标签?或者有可能还有另外一种方法?

解决方案:

?$ / b>

解决方案

根据 W3标准,对于要提交的输入字段,它必须具有名称属性。 Firefox 3和Safari 3.2的快速测试表明,没有提交缺少name属性但具有id属性的输入字段。



有了这个说法,如果你有一个你想要提交的表单,并且它的一些字段有id而不是name属性,使用id属性看起来像是唯一可用的选项。它可能是其他浏览器使用id属性,或者有一些JavaScript代码处理提交事件,而不是让浏览器执行它。


Background:

Using urllib and urllib2 in Python, you can do a form submission.

You first create a dictionary.

formdictionary = { 'search' : 'stackoverflow' }

Then you use urlencode method of urllib to transform this dictionary.

params = urllib.urlencode(formdictionary)

You can now make a url request with urllib2 and pass the variable params as a secondary parameter with the first parameter being the url.

open = urllib2.urlopen('www.searchpage.com', params)

From my understanding, urlencode automatically encodes the dictionary in html and adds the input tag. It takes the key to be the name attribute. It takes value in the dictionary to be the value of the name attribute. Urllib2 send this html code via an HTTP POST request.

Problem:

This is alright if the html code you are submitting to is formatted in a standard way with the html tag input having the name attribute.

<input id="32324" type="text" name="search" >

But, there is the situation where the html code is not properly formatted. And the html input tag only has an id attribute no name attribute. Is there may be another way to access the input tag via the id attribute? Or is there may be yet another way?

Solution:

?

解决方案

According to the W3 standard, for an input field to be submitted, it must have a name attribute. A quick test on Firefox 3 and Safari 3.2 shows that an input field that is missing the name attribute but has an id attribute is not submitted.

With that said, if you have a form that you want to submit, and some of its fields have id but not name attributes, using the id attribute instead seems like the only available option. It could be that other browsers use the id attribute, or perhaps there is some JavaScript code that handles the submission event instead of letting the browser do it.

这篇关于在没有名称属性的Python中提交表单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆