更有效地编写HTTP脚本

Zom*_*ies 8 ruby python scripting perl http

通常我想自动化http查询.我目前使用Java(和commons http客户端),但可能更喜欢基于脚本的方法.一些非常快速和简单的东西.我可以在哪里设置标题,转到页面而不用担心设置整个OO生命周期,设置每个标题,调用html解析器...我正在寻找任何语言的解决方案,更喜欢脚本

emi*_*mil 6

Mechanize for Python似乎很容易使用:http://wwwsearch.sourceforge.net/mechanize/

  • 它似乎也存在于perl中. (2认同)

jbo*_*chi 6

看看Selenium.如果您需要自定义脚本,它会为C#,Java,Perl,PHP,Python和Ruby生成代码.


Nou*_*him 6

Watir听起来接近你想要的,虽然它(如Selenium在另一个答案中链接)实际上打开了一个浏览器来做东西.你可以在这里看到一些例子.另一种基于浏览器的记录+回放方法系统是sahi.

如果您的应用程序使用WSGI,那么粘贴是一个不错的选择.

在另一个答案中链接的Mechanize是"库中的浏览器",perl,RubyPython中都有克隆.Perl one是原始的,如果你想要浏览器,这似乎是要走的路.这种方法的问题是所有前端代码(可能依赖于JavaScript)都不会被执行.


Aif*_*Aif 6

轮到我了:ww或perl with lwp.您将在链接页面上找到示例.


dao*_*oad 6

如果你有简单的需求(获取页面然后解析它),那么很难击败LWP :: SimpleHTML :: TreeBuilder.

use strict;
use warnings;

use LWP::Simple;
use HTML::TreeBuilder;

my $url = 'http://www.example.com';
my $content = get( $url) or die "Couldn't get $url";

my $t = HTML::TreeBuilder->new_from_content( $content );
$t->eof;
$t->elementify;

# Get first match:
my $thing = $t->look_down( _tag => 'p', id => qr/match_this_regex/ );

print $thing ? $thing->as_text : "No match found\n";

# Get all matches:
my @things = $t->look_down( _tag => 'p', id => qr/match_this_regex/ );

print $_ ? $_->as_text : "No match found" for @things;
Run Code Online (Sandbox Code Playgroud)