Sha256: 14d12352337d3c51b72837c5c5860d08b419f744544aedf60b79aceda456c642

Contents?: true

Size: 1.81 KB

Versions: 1

Compression:

Stored size: 1.81 KB

Contents

## xp

Ruby gem that adds 4 new methods to String class that enables easy scraping of HTML/XML documents.

### Installation

    $ gem install xp

### Usage

    $ curl -s 'https://news.ycombinator.com' | xp --text '//td[class="title"]/a'

OR

    $ curl -s 'https://news.ycombinator.com' | xp --text 'td.title > a'

The gem can also be used in Ruby scripts, by `requiring` the gem - `require 'xp'`.

### Example

The following one liner can download all Dribbble shots from its home page:

``` ruby
'https://dribbble.com/'.css('.dribbble-link img').xpath('//img/@src').map { |link| link.text.download }
```

### API

`xp` adds the following methods to the `String` class:

Method                                                         | Return type               | Remarks
-------------------------------------------------------------- | ------------------------- | -------------------------------------------------------------------------------
`to_nokogiri`                                                  | `Nokogiri::XML::Document` | Converts a url or a page source to Nokogiri object
`css(selector)`                                                | `String`                  | Filters a url or html string based on the selector
`xpath(selector)`                                              | `Strng`                   | Filters a url or html string based on the selector
`download(location: 'downloads', name: nil)`                   | `String`                  | Downloads the url in the string; can be customized via the optional parameters.
`page_source(user_agent_alias: :mac_firefox, user_agent: nil)` | `String`                  | Gets the page source of a url; can be customized via optional parameters.
`url?`                                                         | `Boolean`                 | Checks whether current string is a url.

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
xp-2.0.0 README.markdown