juknife 0.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/.gitignore +12 -0
- data/.rspec +2 -0
- data/.rubocop.yml +9 -0
- data/.travis.yml +8 -0
- data/.yardopts +3 -0
- data/CODE_OF_CONDUCT.md +74 -0
- data/Gemfile +6 -0
- data/LICENSE +21 -0
- data/README.md +74 -0
- data/Rakefile +8 -0
- data/bin/console +12 -0
- data/bin/setup +8 -0
- data/juknife.gemspec +37 -0
- data/lib/juknife/request/dsl.rb +32 -0
- data/lib/juknife/request/runner.rb +49 -0
- data/lib/juknife/request.rb +9 -0
- data/lib/juknife/scraper.rb +28 -0
- data/lib/juknife/scraping/context.rb +27 -0
- data/lib/juknife/scraping/dsl/item.rb +28 -0
- data/lib/juknife/scraping/dsl/items.rb +44 -0
- data/lib/juknife/scraping/dsl/scope.rb +30 -0
- data/lib/juknife/scraping/dsl.rb +32 -0
- data/lib/juknife/scraping/runner.rb +38 -0
- data/lib/juknife/scraping.rb +10 -0
- data/lib/juknife/version.rb +5 -0
- data/lib/juknife.rb +10 -0
- metadata +197 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: d90b4e20d8de63f527ae0dc9ab8a2f8e83c82b1e
|
4
|
+
data.tar.gz: 11af76a79256daf047199e3506fd568c7fdb19af
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 9acbf75fbd01ede4751f242aca4f13dc69973c9483d24c9fe6bdb0d490e610d2a78e6b24c31749199a7489355e7d39c6f88989aa5e99df6a37cad924a6ec419b
|
7
|
+
data.tar.gz: 2f9b1104a0fb20aabf1a28fa572603352c877b19e8a91ec2c2ad46f77a85248975ef04e5d08fc9d748f6cbd57a659491b01caa9178a943b46b45b76f42425de2
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.rubocop.yml
ADDED
data/.travis.yml
ADDED
data/.yardopts
ADDED
data/CODE_OF_CONDUCT.md
ADDED
@@ -0,0 +1,74 @@
|
|
1
|
+
# Contributor Covenant Code of Conduct
|
2
|
+
|
3
|
+
## Our Pledge
|
4
|
+
|
5
|
+
In the interest of fostering an open and welcoming environment, we as
|
6
|
+
contributors and maintainers pledge to making participation in our project and
|
7
|
+
our community a harassment-free experience for everyone, regardless of age, body
|
8
|
+
size, disability, ethnicity, gender identity and expression, level of experience,
|
9
|
+
nationality, personal appearance, race, religion, or sexual identity and
|
10
|
+
orientation.
|
11
|
+
|
12
|
+
## Our Standards
|
13
|
+
|
14
|
+
Examples of behavior that contributes to creating a positive environment
|
15
|
+
include:
|
16
|
+
|
17
|
+
* Using welcoming and inclusive language
|
18
|
+
* Being respectful of differing viewpoints and experiences
|
19
|
+
* Gracefully accepting constructive criticism
|
20
|
+
* Focusing on what is best for the community
|
21
|
+
* Showing empathy towards other community members
|
22
|
+
|
23
|
+
Examples of unacceptable behavior by participants include:
|
24
|
+
|
25
|
+
* The use of sexualized language or imagery and unwelcome sexual attention or
|
26
|
+
advances
|
27
|
+
* Trolling, insulting/derogatory comments, and personal or political attacks
|
28
|
+
* Public or private harassment
|
29
|
+
* Publishing others' private information, such as a physical or electronic
|
30
|
+
address, without explicit permission
|
31
|
+
* Other conduct which could reasonably be considered inappropriate in a
|
32
|
+
professional setting
|
33
|
+
|
34
|
+
## Our Responsibilities
|
35
|
+
|
36
|
+
Project maintainers are responsible for clarifying the standards of acceptable
|
37
|
+
behavior and are expected to take appropriate and fair corrective action in
|
38
|
+
response to any instances of unacceptable behavior.
|
39
|
+
|
40
|
+
Project maintainers have the right and responsibility to remove, edit, or
|
41
|
+
reject comments, commits, code, wiki edits, issues, and other contributions
|
42
|
+
that are not aligned to this Code of Conduct, or to ban temporarily or
|
43
|
+
permanently any contributor for other behaviors that they deem inappropriate,
|
44
|
+
threatening, offensive, or harmful.
|
45
|
+
|
46
|
+
## Scope
|
47
|
+
|
48
|
+
This Code of Conduct applies both within project spaces and in public spaces
|
49
|
+
when an individual is representing the project or its community. Examples of
|
50
|
+
representing a project or community include using an official project e-mail
|
51
|
+
address, posting via an official social media account, or acting as an appointed
|
52
|
+
representative at an online or offline event. Representation of a project may be
|
53
|
+
further defined and clarified by project maintainers.
|
54
|
+
|
55
|
+
## Enforcement
|
56
|
+
|
57
|
+
Instances of abusive, harassing, or otherwise unacceptable behavior may be
|
58
|
+
reported by contacting the project team at nyamadorig@gmail.com. All
|
59
|
+
complaints will be reviewed and investigated and will result in a response that
|
60
|
+
is deemed necessary and appropriate to the circumstances. The project team is
|
61
|
+
obligated to maintain confidentiality with regard to the reporter of an incident.
|
62
|
+
Further details of specific enforcement policies may be posted separately.
|
63
|
+
|
64
|
+
Project maintainers who do not follow or enforce the Code of Conduct in good
|
65
|
+
faith may face temporary or permanent repercussions as determined by other
|
66
|
+
members of the project's leadership.
|
67
|
+
|
68
|
+
## Attribution
|
69
|
+
|
70
|
+
This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
|
71
|
+
available at [http://contributor-covenant.org/version/1/4][version]
|
72
|
+
|
73
|
+
[homepage]: http://contributor-covenant.org
|
74
|
+
[version]: http://contributor-covenant.org/version/1/4/
|
data/Gemfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
MIT License
|
2
|
+
|
3
|
+
Copyright (c) 2017 nyamadori
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
13
|
+
copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
21
|
+
SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,74 @@
|
|
1
|
+
# Juknife
|
2
|
+
|
3
|
+
[](https://travis-ci.org/nyamadori/juknife)
|
4
|
+
|
5
|
+
A Ruby library for Web scraping. It allows to describe scraping nodes and text structurally and declaratively.
|
6
|
+
|
7
|
+
For example, scraping on Google search can be written as follows:
|
8
|
+
|
9
|
+
```ruby
|
10
|
+
class GoogleSearchScraper < Juknife::Scraper
|
11
|
+
request do
|
12
|
+
get 'https://www.google.co.jp/search'
|
13
|
+
user_agent 'Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.0)'
|
14
|
+
query do
|
15
|
+
{
|
16
|
+
q: params[:query]
|
17
|
+
}
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
scraping do
|
22
|
+
items :results, '#ires .g' do
|
23
|
+
item :title, 'h3'
|
24
|
+
item :url, '.kv > cite'
|
25
|
+
item :description, '.st'
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
scraper = GoogleSearchScraper.new
|
31
|
+
scraper.scrape(query: 'test')
|
32
|
+
# =>
|
33
|
+
# {:results=>
|
34
|
+
# [
|
35
|
+
# {:title=>"testの意味・用例|英辞郎 on the WEB:アルク",
|
36
|
+
# :url=>"https://eow.alc.co.jp/search?q=test",
|
37
|
+
# :description=>
|
38
|
+
# "test 【1自動】 試験[検査]を受ける 《医・化学》検査[分析]を行う 〔試験で... - アルクがお\n" +
|
39
|
+
# "届けする進化するオンライン英和・和英辞書データベース。一般的な単語や連語から、\n" +
|
40
|
+
# "イディオム、専門用語、スラングまで幅広く収録。"}
|
41
|
+
# }
|
42
|
+
# ]
|
43
|
+
# }
|
44
|
+
```
|
45
|
+
|
46
|
+
## Installation
|
47
|
+
|
48
|
+
Add this line to your application's Gemfile:
|
49
|
+
|
50
|
+
```ruby
|
51
|
+
gem 'juknife'
|
52
|
+
```
|
53
|
+
|
54
|
+
And then execute:
|
55
|
+
|
56
|
+
$ bundle
|
57
|
+
|
58
|
+
Or install it yourself as:
|
59
|
+
|
60
|
+
$ gem install juknife
|
61
|
+
|
62
|
+
## Usage
|
63
|
+
|
64
|
+
[TODO]
|
65
|
+
|
66
|
+
## Development
|
67
|
+
|
68
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
69
|
+
|
70
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
71
|
+
|
72
|
+
## Contributing
|
73
|
+
|
74
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/juknife. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,12 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
# frozen_string_literal: true
|
3
|
+
|
4
|
+
require 'bundler/setup'
|
5
|
+
require 'juknife'
|
6
|
+
|
7
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
8
|
+
# with your gem easier. You can also use a different console, if you like.
|
9
|
+
|
10
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
11
|
+
require 'pry'
|
12
|
+
Pry.start
|
data/bin/setup
ADDED
data/juknife.gemspec
ADDED
@@ -0,0 +1,37 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
# frozen_string_literal: true
|
3
|
+
|
4
|
+
lib = File.expand_path('../lib', __FILE__)
|
5
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
6
|
+
require 'juknife/version'
|
7
|
+
|
8
|
+
Gem::Specification.new do |spec|
|
9
|
+
spec.name = 'juknife'
|
10
|
+
spec.version = Juknife::VERSION
|
11
|
+
spec.authors = ['nyamadori']
|
12
|
+
spec.email = ['nyamadorig@gmail.com']
|
13
|
+
|
14
|
+
spec.summary = 'A Ruby library for Web scraping.'
|
15
|
+
spec.description = <<~DESC
|
16
|
+
A Ruby library for Web scraping. It allows to describe scraping nodes and
|
17
|
+
text structurally and declaratively.
|
18
|
+
DESC
|
19
|
+
spec.homepage = 'https://github.com/nyamadori/juknife'
|
20
|
+
|
21
|
+
spec.files = `git ls-files -z`.split("\x0").reject do |f|
|
22
|
+
f.match(%r{^(test|spec|features)/})
|
23
|
+
end
|
24
|
+
spec.bindir = 'exe'
|
25
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
26
|
+
spec.require_paths = ['lib']
|
27
|
+
|
28
|
+
spec.add_runtime_dependency 'activesupport', '~> 5.0.2'
|
29
|
+
spec.add_runtime_dependency 'nokogiri', '~> 1.7'
|
30
|
+
spec.add_runtime_dependency 'faraday', '~> 0.1'
|
31
|
+
spec.add_development_dependency 'bundler', '~> 1.14'
|
32
|
+
spec.add_development_dependency 'rake', '~> 10.0'
|
33
|
+
spec.add_development_dependency 'rspec', '~> 3.0'
|
34
|
+
spec.add_development_dependency 'rubocop'
|
35
|
+
spec.add_development_dependency 'yard'
|
36
|
+
spec.add_development_dependency 'pry'
|
37
|
+
end
|
@@ -0,0 +1,32 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Juknife
|
4
|
+
module Request
|
5
|
+
# A DSL for definition target web page and request methods.
|
6
|
+
module DSL
|
7
|
+
HTTP_METHODS = %i[get post put delete head patch options].freeze
|
8
|
+
|
9
|
+
attr_reader :http_method, :url_builder, :body_builder, :user_agent_builder
|
10
|
+
attr_reader :query_builder
|
11
|
+
|
12
|
+
HTTP_METHODS.each do |method|
|
13
|
+
define_method(method) do |url, &block|
|
14
|
+
@http_method = method
|
15
|
+
@url_builder = block || proc { url }
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
def body(&block)
|
20
|
+
@body_builder = block
|
21
|
+
end
|
22
|
+
|
23
|
+
def user_agent(ua = nil, &block)
|
24
|
+
@user_agent_builder = block || proc { ua }
|
25
|
+
end
|
26
|
+
|
27
|
+
def query(&block)
|
28
|
+
@query_builder = block
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
@@ -0,0 +1,49 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'faraday'
|
4
|
+
|
5
|
+
module Juknife
|
6
|
+
module Request
|
7
|
+
# Juknife::Request::Runner object is a runner for request DSL.
|
8
|
+
class Runner
|
9
|
+
include DSL
|
10
|
+
attr_reader :block, :params
|
11
|
+
|
12
|
+
def initialize(&block)
|
13
|
+
@block = block
|
14
|
+
instance_eval(&@block)
|
15
|
+
end
|
16
|
+
|
17
|
+
def run(params = {})
|
18
|
+
@params = params
|
19
|
+
source
|
20
|
+
end
|
21
|
+
|
22
|
+
def source
|
23
|
+
@source ||= response.body
|
24
|
+
end
|
25
|
+
|
26
|
+
private
|
27
|
+
|
28
|
+
def response
|
29
|
+
@response ||= rack_builder.build_response(connection, request)
|
30
|
+
end
|
31
|
+
|
32
|
+
def connection
|
33
|
+
@connection ||=
|
34
|
+
Faraday.new(url_builder.call, params: query_builder.call)
|
35
|
+
end
|
36
|
+
|
37
|
+
def rack_builder
|
38
|
+
@builder ||= Faraday::RackBuilder.new
|
39
|
+
end
|
40
|
+
|
41
|
+
def request
|
42
|
+
@request ||= connection.build_request(http_method) do |req|
|
43
|
+
req.headers['User-Agent'] = user_agent_builder&.call
|
44
|
+
req.body = body_builder&.call
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
@@ -0,0 +1,28 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Juknife
|
4
|
+
# Juknife::Scraper is a scraper to request a web page and to extract data.
|
5
|
+
class Scraper
|
6
|
+
def initialize
|
7
|
+
@scraping = Scraping::Runner.new(&self.class.scraping_block)
|
8
|
+
@request = Request::Runner.new(&self.class.request_block)
|
9
|
+
end
|
10
|
+
|
11
|
+
def scrape(params = {})
|
12
|
+
source = @request.run(params)
|
13
|
+
@scraping.run(source, params)
|
14
|
+
end
|
15
|
+
|
16
|
+
class << self
|
17
|
+
attr_reader :scraping_block, :request_block
|
18
|
+
|
19
|
+
def scraping(&block)
|
20
|
+
@scraping_block = block
|
21
|
+
end
|
22
|
+
|
23
|
+
def request(&block)
|
24
|
+
@request_block = block
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
@@ -0,0 +1,27 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Juknife
|
4
|
+
module Scraping
|
5
|
+
# A context object keeps a state of scraping.
|
6
|
+
class Context
|
7
|
+
attr_reader :node, :result
|
8
|
+
|
9
|
+
def initialize(node, result = {})
|
10
|
+
@node = node
|
11
|
+
@result = result
|
12
|
+
end
|
13
|
+
|
14
|
+
def find(selector)
|
15
|
+
node.at_css(selector)
|
16
|
+
end
|
17
|
+
|
18
|
+
def find_all(selector)
|
19
|
+
node.css(selector)
|
20
|
+
end
|
21
|
+
|
22
|
+
def text(selector)
|
23
|
+
find(selector)&.text&.strip&.gsub(/(\s)(\s*)/, '\1')
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
@@ -0,0 +1,28 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Juknife
|
4
|
+
module Scraping
|
5
|
+
module DSL
|
6
|
+
# A DSL node in the tree that scrapes an element.
|
7
|
+
class Item
|
8
|
+
include DSL
|
9
|
+
|
10
|
+
attr_reader :name, :selector, :type
|
11
|
+
|
12
|
+
def initialize(name, selector, type = :string, *args, &block)
|
13
|
+
@name = name
|
14
|
+
@selector = selector
|
15
|
+
@type = type
|
16
|
+
@args = args
|
17
|
+
|
18
|
+
return unless block
|
19
|
+
instance_eval(&block)
|
20
|
+
end
|
21
|
+
|
22
|
+
def visit(context)
|
23
|
+
context.result[name] = context.text(selector)
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
@@ -0,0 +1,44 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'active_support'
|
4
|
+
require 'active_support/core_ext/object'
|
5
|
+
|
6
|
+
module Juknife
|
7
|
+
module Scraping
|
8
|
+
module DSL
|
9
|
+
# A DSL node in the tree that scrapes elements.
|
10
|
+
class Items
|
11
|
+
include DSL
|
12
|
+
|
13
|
+
attr_reader :name, :selector, :type
|
14
|
+
|
15
|
+
def initialize(name, selector, type = :string, *args, &block)
|
16
|
+
@name = name
|
17
|
+
@selector = selector
|
18
|
+
@type = type
|
19
|
+
@args = args
|
20
|
+
|
21
|
+
return unless block
|
22
|
+
instance_eval(&block)
|
23
|
+
end
|
24
|
+
|
25
|
+
def visit(context) # rubocop:disable Metrics/AbcSize
|
26
|
+
result = []
|
27
|
+
|
28
|
+
context.find_all(selector).each do |ele_chlid|
|
29
|
+
child_context = Context.new(ele_chlid, {})
|
30
|
+
|
31
|
+
children.each do |child|
|
32
|
+
child.visit(child_context)
|
33
|
+
end
|
34
|
+
|
35
|
+
result << child_context.result
|
36
|
+
end
|
37
|
+
|
38
|
+
context.result[name] =
|
39
|
+
result.reject { |hash| hash.values.compact.all?(&:empty?) }
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
@@ -0,0 +1,30 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Juknife
|
4
|
+
module Scraping
|
5
|
+
module DSL
|
6
|
+
# A DSL node in the tree that makes an scope of element.
|
7
|
+
class Scope
|
8
|
+
include DSL
|
9
|
+
|
10
|
+
attr_reader :selector, :args
|
11
|
+
|
12
|
+
def initialize(selector, *args, &block)
|
13
|
+
@selector = selector
|
14
|
+
@args = args
|
15
|
+
|
16
|
+
return unless block
|
17
|
+
instance_eval(&block)
|
18
|
+
end
|
19
|
+
|
20
|
+
def visit(context)
|
21
|
+
scope = context.find(selector)
|
22
|
+
|
23
|
+
children.each do |child|
|
24
|
+
child.visit(Context.new(scope, context.result))
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
@@ -0,0 +1,32 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require_relative 'dsl/item'
|
4
|
+
require_relative 'dsl/items'
|
5
|
+
require_relative 'dsl/scope'
|
6
|
+
|
7
|
+
module Juknife
|
8
|
+
module Scraping
|
9
|
+
# A DSL for definition scraping items on the web page.
|
10
|
+
module DSL
|
11
|
+
autoload :Item, 'juknife/scraping/dsl/item'
|
12
|
+
autoload :Items, 'juknife/scraping/dsl/items'
|
13
|
+
autoload :Scope, 'juknife/scraping/dsl/scope'
|
14
|
+
|
15
|
+
def item(name, *args, &block)
|
16
|
+
children << Item.new(name, *args, &block)
|
17
|
+
end
|
18
|
+
|
19
|
+
def items(name, *args, &block)
|
20
|
+
children << Items.new(name, *args, &block)
|
21
|
+
end
|
22
|
+
|
23
|
+
def scope(name, *args, &block)
|
24
|
+
children << Scope.new(name, *args, &block)
|
25
|
+
end
|
26
|
+
|
27
|
+
def children
|
28
|
+
@children ||= []
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
@@ -0,0 +1,38 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'nokogiri'
|
4
|
+
|
5
|
+
module Juknife
|
6
|
+
module Scraping
|
7
|
+
# Juknife::Scraping::Runner object is a runner for scraping DSL.
|
8
|
+
class Runner
|
9
|
+
include DSL
|
10
|
+
attr_reader :source, :block, :params
|
11
|
+
|
12
|
+
def initialize(&block)
|
13
|
+
@block = block
|
14
|
+
instance_eval(&@block)
|
15
|
+
end
|
16
|
+
|
17
|
+
def run(source, params = {})
|
18
|
+
@params = params
|
19
|
+
@source = source
|
20
|
+
result
|
21
|
+
end
|
22
|
+
|
23
|
+
def document
|
24
|
+
@document ||= Nokogiri.parse(source)
|
25
|
+
end
|
26
|
+
|
27
|
+
def result
|
28
|
+
context = Scraping::Context.new(document)
|
29
|
+
|
30
|
+
children.each do |child|
|
31
|
+
child.visit(context)
|
32
|
+
end
|
33
|
+
|
34
|
+
context.result
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
data/lib/juknife.rb
ADDED
@@ -0,0 +1,10 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'juknife/version'
|
4
|
+
|
5
|
+
# The module that contains everything Juknife-related.
|
6
|
+
module Juknife
|
7
|
+
autoload :Request, 'juknife/request'
|
8
|
+
autoload :Scraper, 'juknife/scraper'
|
9
|
+
autoload :Scraping, 'juknife/scraping'
|
10
|
+
end
|
metadata
ADDED
@@ -0,0 +1,197 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: juknife
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- nyamadori
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2017-05-07 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: activesupport
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: 5.0.2
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: 5.0.2
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: nokogiri
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '1.7'
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '1.7'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: faraday
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0.1'
|
48
|
+
type: :runtime
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0.1'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: bundler
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '1.14'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '1.14'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: rake
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - "~>"
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '10.0'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - "~>"
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '10.0'
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: rspec
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - "~>"
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '3.0'
|
90
|
+
type: :development
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - "~>"
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: '3.0'
|
97
|
+
- !ruby/object:Gem::Dependency
|
98
|
+
name: rubocop
|
99
|
+
requirement: !ruby/object:Gem::Requirement
|
100
|
+
requirements:
|
101
|
+
- - ">="
|
102
|
+
- !ruby/object:Gem::Version
|
103
|
+
version: '0'
|
104
|
+
type: :development
|
105
|
+
prerelease: false
|
106
|
+
version_requirements: !ruby/object:Gem::Requirement
|
107
|
+
requirements:
|
108
|
+
- - ">="
|
109
|
+
- !ruby/object:Gem::Version
|
110
|
+
version: '0'
|
111
|
+
- !ruby/object:Gem::Dependency
|
112
|
+
name: yard
|
113
|
+
requirement: !ruby/object:Gem::Requirement
|
114
|
+
requirements:
|
115
|
+
- - ">="
|
116
|
+
- !ruby/object:Gem::Version
|
117
|
+
version: '0'
|
118
|
+
type: :development
|
119
|
+
prerelease: false
|
120
|
+
version_requirements: !ruby/object:Gem::Requirement
|
121
|
+
requirements:
|
122
|
+
- - ">="
|
123
|
+
- !ruby/object:Gem::Version
|
124
|
+
version: '0'
|
125
|
+
- !ruby/object:Gem::Dependency
|
126
|
+
name: pry
|
127
|
+
requirement: !ruby/object:Gem::Requirement
|
128
|
+
requirements:
|
129
|
+
- - ">="
|
130
|
+
- !ruby/object:Gem::Version
|
131
|
+
version: '0'
|
132
|
+
type: :development
|
133
|
+
prerelease: false
|
134
|
+
version_requirements: !ruby/object:Gem::Requirement
|
135
|
+
requirements:
|
136
|
+
- - ">="
|
137
|
+
- !ruby/object:Gem::Version
|
138
|
+
version: '0'
|
139
|
+
description: |
|
140
|
+
A Ruby library for Web scraping. It allows to describe scraping nodes and
|
141
|
+
text structurally and declaratively.
|
142
|
+
email:
|
143
|
+
- nyamadorig@gmail.com
|
144
|
+
executables: []
|
145
|
+
extensions: []
|
146
|
+
extra_rdoc_files: []
|
147
|
+
files:
|
148
|
+
- ".gitignore"
|
149
|
+
- ".rspec"
|
150
|
+
- ".rubocop.yml"
|
151
|
+
- ".travis.yml"
|
152
|
+
- ".yardopts"
|
153
|
+
- CODE_OF_CONDUCT.md
|
154
|
+
- Gemfile
|
155
|
+
- LICENSE
|
156
|
+
- README.md
|
157
|
+
- Rakefile
|
158
|
+
- bin/console
|
159
|
+
- bin/setup
|
160
|
+
- juknife.gemspec
|
161
|
+
- lib/juknife.rb
|
162
|
+
- lib/juknife/request.rb
|
163
|
+
- lib/juknife/request/dsl.rb
|
164
|
+
- lib/juknife/request/runner.rb
|
165
|
+
- lib/juknife/scraper.rb
|
166
|
+
- lib/juknife/scraping.rb
|
167
|
+
- lib/juknife/scraping/context.rb
|
168
|
+
- lib/juknife/scraping/dsl.rb
|
169
|
+
- lib/juknife/scraping/dsl/item.rb
|
170
|
+
- lib/juknife/scraping/dsl/items.rb
|
171
|
+
- lib/juknife/scraping/dsl/scope.rb
|
172
|
+
- lib/juknife/scraping/runner.rb
|
173
|
+
- lib/juknife/version.rb
|
174
|
+
homepage: https://github.com/nyamadori/juknife
|
175
|
+
licenses: []
|
176
|
+
metadata: {}
|
177
|
+
post_install_message:
|
178
|
+
rdoc_options: []
|
179
|
+
require_paths:
|
180
|
+
- lib
|
181
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
182
|
+
requirements:
|
183
|
+
- - ">="
|
184
|
+
- !ruby/object:Gem::Version
|
185
|
+
version: '0'
|
186
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
187
|
+
requirements:
|
188
|
+
- - ">="
|
189
|
+
- !ruby/object:Gem::Version
|
190
|
+
version: '0'
|
191
|
+
requirements: []
|
192
|
+
rubyforge_project:
|
193
|
+
rubygems_version: 2.6.12
|
194
|
+
signing_key:
|
195
|
+
specification_version: 4
|
196
|
+
summary: A Ruby library for Web scraping.
|
197
|
+
test_files: []
|