是否有像Perl的LWP for Ruby这样的模块? [英] Is there a module like Perl's LWP for Ruby?

查看:126
本文介绍了是否有像Perl的LWP for Ruby这样的模块?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Perl中有一个 LWP模块


libwww-perl集合是一组Perl模块,它向万维网提供简单而一致的应用程序编程接口(API)。该库的主要重点是提供允许您编写WWW客户端的类和函数。该库还包含更通用的模块,甚至可以帮助您实现简单的HTTP服务器的类。


是否有类似的模块(gem)for Ruby?



更新

下面是一个例子函数我已经从一个特定的网站提取URL。

 使用LWP :: UserAgent; 
使用HTML :: TreeBuilder 3;
使用HTML :: TokeParser;

get_gallery_urls {
my $ url = shift;

my $ ua = LWP :: UserAgent-> new;
$ ua->代理($ 0 / 0.1。$ ua->代理);
$ ua->代理(Mozilla / 8.0);

my $ req = new HTTP :: Request'GET'=> $网址;
$ req-> header('Accept'=>'text / html');

#发送请求
$ response_u = $ ua->请求($ req);

dieError:,$ response_u-> status_line unless $ response_u-> is_success;

my $ root = HTML :: TreeBuilder-> new;
$ root-> parse($ response_u-> content);

my @gu = $ root-> find_by_attribute(id,thumbnails);

my%url =();

foreach my $ g(@gu){
my @as = $ g-> find_by_tag_name('a');

foreach $ a(@as){
my $ u = $ a-> attr(href);

if($ u =〜/ ^ \ //){
$ url {http://example.com。$ u} = 1;
}
}
}

返回%网址;


解决方案

一个href =http://rubygems.org/gems/httpclient> httpclient ,其目标是相当于LWP。不过,根据你打算做什么,可能会有更好的选择。如果您打算遵循链接,填写表格等,以便抓取网页内容,您可以使用机械化,这与perl模块由相同的名称。还有更多特定于Ruby的宝石,例如优秀的 Rest-client HTTParty (我个人最喜欢的)。请参阅 Ruby工具箱的HTTP客户端类别获取更多列表。



更新:下面是一个如何在Mechanize的页面上查找所有链接的示例(Ruby,但它在Perl中类似):

  require'rubygems'
要求'机械化'

代理= Mechanize.new

页面=代理。 get('http://example.com/')

page.links.each do | link |
puts link.text
end

作为一名前Perler,我曾经担心放弃优秀的CPAN - 我会把自己与Ruby相提并论吗?我将无法找到与我所依赖的模块相同的内容吗?事实证明,这根本不成问题,事实上最近事实恰恰相反:Ruby(与Python一起)倾向于首先获得客户端对新平台/ Web服务等的支持。


In Perl there is an LWP module:

The libwww-perl collection is a set of Perl modules which provides a simple and consistent application programming interface (API) to the World-Wide Web. The main focus of the library is to provide classes and functions that allow you to write WWW clients. The library also contain modules that are of more general use and even classes that help you implement simple HTTP servers.

Is there a similar module (gem) for Ruby?

Update

Here is an example of a function I have made that extracts URL's from a specific website.

use LWP::UserAgent;
use HTML::TreeBuilder 3;
use HTML::TokeParser;

sub get_gallery_urls {
    my $url = shift;

    my $ua = LWP::UserAgent->new;
    $ua->agent("$0/0.1 " . $ua->agent);
    $ua->agent("Mozilla/8.0");

    my $req = new HTTP::Request 'GET' => "$url";
    $req->header('Accept' => 'text/html');

    # send request
    $response_u = $ua->request($req);

    die "Error: ", $response_u->status_line unless $response_u->is_success;

    my $root = HTML::TreeBuilder->new;
    $root->parse($response_u->content);

    my @gu = $root->find_by_attribute("id", "thumbnails");

    my %urls = ();

    foreach my $g (@gu) {
        my @as = $g->find_by_tag_name('a');

        foreach $a (@as) {
            my $u = $a->attr("href");

            if ($u =~ /^\//) {
                $urls{"http://example.com"."$u"} = 1;
            }
        }
    }

    return %urls;
}

解决方案

The closest match is probably httpclient, which aims to be the equivalent of LWP. However, depending on what you plan to do, there may be better options. If you plan to follow links, fill out forms, etc. in order to scrape web content, you can use Mechanize which is similar to the perl module by the same name. There are also more Ruby-specific gems, such as the excellent Rest-client and HTTParty (my personal favorite). See the HTTP Clients category of Ruby Toolbox for a larger list.

Update: Here's an example of how to find all links on a page in Mechanize (Ruby, but it would be similar in Perl):

require 'rubygems'
require 'mechanize'

agent = Mechanize.new

page = agent.get('http://example.com/')

page.links.each do |link|
  puts link.text
end

P.S. As an ex-Perler myself, I used to worry about abandoning the excellent CPAN--would I paint myself into a corner with Ruby? Would I not be able to find an equivalent to a module I rely on? This has turned out not to be a problem at all, and in fact lately has been quite the opposite: Ruby (along with Python) tends to be the first to get client support for new platforms/web services, etc.

这篇关于是否有像Perl的LWP for Ruby这样的模块?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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