如何处理网站上的无名表单?

发布于 2024-10-25 05:12:29 字数 1115 浏览 1 评论 0原文

我想编写一个脚本,让我使用这个网站

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 技术交流群。

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

发布评论

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

评论(3

明明#如月 2024-11-01 05:12:32

我不知道 WWW::Mechanize,但它的 Python 等效项 mechanize,给你一个表单数组,即使你不知道它们的名字,你也可以迭代它们。

示例(取自其主页):

import mechanize

br = mechanize.Browser()
br.open("http://www.example.com/")

for form in br.forms():
    print form

编辑: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):

import mechanize

br = mechanize.Browser()
br.open("http://www.example.com/")

for form in br.forms():
    print form

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 or WWW::Mechanize, I'll leave there my python answer.

清晰传感 2024-11-01 05:12:32

好吧,我已经找到答案了。我可以通过编号来寻址无名表单(网页上只有一个表单,所以我猜它会是编号 1,并且它有效)。这是我的代码的一部分:

my $lga = WWW::Mechanize->new();

my $address = '[email protected]';
my $options = '-3 -o0 -d:4.0';
my $pdb_2   = "${pdb_id}_1 ${pdb_id}_2";

$lga->get('http://proteinmodel.org/AS2TS/LGA/lga.html');
$lga->success or die "LGA GET fail\n";

$lga->form_number(1);
$lga->field('Address', $address);
$lga->field('Options', $options);
$lga->field('PDB_2', $pdb_2);
$lga->submit();
$lga->success or die "LGA POST fail\n";

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:

my $lga = WWW::Mechanize->new();

my $address = '[email protected]';
my $options = '-3 -o0 -d:4.0';
my $pdb_2   = "${pdb_id}_1 ${pdb_id}_2";

$lga->get('http://proteinmodel.org/AS2TS/LGA/lga.html');
$lga->success or die "LGA GET fail\n";

$lga->form_number(1);
$lga->field('Address', $address);
$lga->field('Options', $options);
$lga->field('PDB_2', $pdb_2);
$lga->submit();
$lga->success or die "LGA POST fail\n";
塔塔猫 2024-11-01 05:12:31

一种简单且更可靠的方法是使用 $mech->form_with_fields() 方法来自 WWW::Mechanize 根据其包含的字段选择您想要的表单。

更简单的是,使用带有 with_fields 选项的 submit_form 方法。

例如,要找到一个包含名为“用户名”和“密码”字段的表单,完成它们并提交表单,就像这样简单:

$mech->submit_form( 
    with_fields => { username => $username, password => $password }
);

这样做的优点是,如果他们打乱 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 the with_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:

$mech->submit_form( 
    with_fields => { username => $username, password => $password }
);

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.

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