如何处理网站上的无名表单?
我想编写一个脚本,让我使用这个网站
http:// Proteinmodel.org/AS2TS/ LGA/lga.html
(我需要使用它几百次,而且我不想手动执行此操作)
我在互联网上搜索了如何使用 Perl 完成此操作的方法,并且我遇到了WWW::Mechanize
,这似乎正是我正在寻找的东西。但现在我发现我想要使用的网站上的表单没有名称 - 它的声明行只是简单地写着:
<FORM METHOD="POST" ACTION="./lga-form.cgi" ENCTYPE=multipart/form-data>
起初我尝试简单地不设置我的 WWW::Mechanize
对象的 form_name< /code> 属性,当我为表单的电子邮件地址字段提供值时,它给了我此错误消息:
Argument "[email protected]" isn't numeric in numeric gt (>) at /usr/share/perl5/WWW/Mechanize.pm line 1618.
然后我尝试将 form_name
设置为 ''
以及后来的 ' '
,但是并没有什么用,我只是得到了这样的信息:
There is no form named " " at ./automate_LGA.pl line 40
有什么办法可以处理没有名字的表单呢?如果这里有人可以回答这个问题,那将是最有帮助的 - 即使答案指向使用 WWW::Mechanize
,因为我只是想完成工作,(或多或少)不不管如何。
预先非常感谢!
I would like to write a script that lets me use this website
http://proteinmodel.org/AS2TS/LGA/lga.html
(I need to use it a few hundred times, and I don't feel like doing that manually)
I have searched the internet for ways how this could be done using Perl, and I came across WWW::Mechanize
, which seemed to be just what I was looking for. But now I have discovered that the form on that website which I want to use has no name - its declaration line simply reads
<FORM METHOD="POST" ACTION="./lga-form.cgi" ENCTYPE=multipart/form-data>
At first I tried simply not setting my WWW::Mechanize
object's form_name
property, which gave me this error message when I provided a value for the form's email address field:
Argument "[email protected]" isn't numeric in numeric gt (>) at /usr/share/perl5/WWW/Mechanize.pm line 1618.
I then tried setting form_name
to ''
and later ' '
, but it was to no avail, I simply got this message:
There is no form named " " at ./automate_LGA.pl line 40
What way is there to deal with forms that have no names? It would be most helpful if someone on here could answer this question - even if the answer points away from using WWW::Mechanize
, as I just want to get the job done, (more or less) no matter how.
Thanks a lot in advance!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我不知道
WWW::Mechanize
,但它的 Python 等效项 mechanize,给你一个表单数组,即使你不知道它们的名字,你也可以迭代它们。示例(取自其主页):
编辑:在
WWW::Mechanize
的文档中搜索我找到了$mech->forms() 方法,这可能就是您所需要的。但由于我不知道 perl 或WWW::Mechanize
,所以我将保留我的 python 答案。I don't know about
WWW::Mechanize
, but its Python equivalent, mechanize, gives you an array of forms that you can iterate even if you don't know their names.Example (taken from its homepage):
EDIT: searching in the docs of
WWW::Mechanize
I found the $mech->forms() method, that could be what you need. But since I don't know perl orWWW::Mechanize
, I'll leave there my python answer.好吧,我已经找到答案了。我可以通过编号来寻址无名表单(网页上只有一个表单,所以我猜它会是编号
1
,并且它有效)。这是我的代码的一部分:Okay, I have found the answer. I can address the nameless form by its number (there's just one form on the webpage, so I guessed it would be number
1
, and it worked). Here's part of my code:一种简单且更可靠的方法是使用 $mech->form_with_fields() 方法来自 WWW::Mechanize 根据其包含的字段选择您想要的表单。
更简单的是,使用带有
with_fields
选项的submit_form
方法。例如,要找到一个包含名为“用户名”和“密码”字段的表单,完成它们并提交表单,就像这样简单:
这样做的优点是,如果他们打乱 HTML 的顺序,则可以更改HTML 中的表单,或者在您感兴趣的表单之前添加一个新表单,您的代码将继续工作。
An easy and more robust way is to use the $mech->form_with_fields() method from WWW::Mechanize to select the form you want based on the fields it contains.
Easier still, use the
submit_form
method with thewith_fields
option.For instance, to locate a form which has fields named 'username' and 'password', complete them and submit the form, it's as easy as:
Doing it this way has the advantage that if they shuffle their HTML around, changing the order of the forms in the HTML, or adding a new form before the one you're interested in, your code will continue to work.