flyerhzm-regexp_crawler 0.1.0 → 0.2.0

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.
data/LICENSE CHANGED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2009 Richard Huang (flyerhzm@gmail.com)
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README CHANGED
@@ -1 +1,20 @@
1
- A crawler which use regrex expression to catch data.
1
+ RegexpCrawler
2
+ ============
3
+
4
+ RegexpCrawler is a crawler which use regrex expression to catch data.
5
+
6
+
7
+ Install
8
+ =======
9
+
10
+ gem sources -a http://gems.github.com
11
+ gem install flyerhzm-regexp_crawler
12
+
13
+
14
+ Usage
15
+ =====
16
+
17
+ >> crawler = RegexpCrawler::Crawler.new(:start_page => "http://www.tijee.com/tags/64-google-face-questions/posts", :continue_regexp => %r{"(/posts/\d+-[^#]*?)"}, :capture_regexp => %r{<h2 class='title'><a.*?>(.*?)</a></h2>.*?<div class='body'>(.*?)</div>}m, :named_captures => ['title', 'body'], :model => Post)
18
+ >> crawler.start
19
+
20
+ =>[{:page=>"http://www.tijee.com/posts/327-google-face-questions-many-companies-will-ask-oh", :model=>#<Post id: nil, title: "Google面试题(很多公司都会问的哦)", body: "\n内容摘要:几星期前,一个朋友接受...", created_at: nil, updated_at: nil, verify: false>}, {:page=>"http://www.tijee.com/posts/328-java-surface-together-with-the-google-test", :model=>#<Post id: nil, title: "google的一道JAVA面试题", body: "\n内容摘要:有一个整数n,写一个函数f(n...", created_at: nil, updated_at: nil, verify: false>}]
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.1.0
1
+ 0.2.0
@@ -16,9 +16,11 @@ module RegexpCrawler
16
16
 
17
17
  def start
18
18
  results = []
19
- @pages = [@start_page]
19
+ @captured_pages = []
20
+ @pages = [URI.parse(@start_page)]
20
21
  while !@pages.empty?
21
- uri = URI.parse(@pages.shift)
22
+ uri = @pages.shift
23
+ @captured_pages << uri
22
24
  result = parse_page(uri)
23
25
  results << result if result
24
26
  end
@@ -33,8 +35,9 @@ module RegexpCrawler
33
35
  def parse_response(response, uri)
34
36
  if response.is_a? Net::HTTPSuccess
35
37
  response.body.scan(continue_regexp).each do |page|
36
- url = page.start_with?(uri.scheme) ? page : "#{uri.scheme}://#{uri.host}/#{page}"
37
- @pages << url
38
+ page = page.first if page.is_a? Array
39
+ continue_uri = page.start_with?(uri.scheme) ? URI.parse(page) : URI.join(uri.scheme + '://' + uri.host, page)
40
+ @pages << continue_uri unless @captured_pages.include?(continue_uri) or @pages.include?(continue_uri)
38
41
  end if continue_regexp
39
42
  md = @capture_regexp.match(response.body)
40
43
  if md
@@ -0,0 +1,54 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ Gem::Specification.new do |s|
4
+ s.name = %q{regexp_crawler}
5
+ s.version = "0.2.0"
6
+
7
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
8
+ s.authors = ["Richard Huang"]
9
+ s.date = %q{2009-08-02}
10
+ s.description = %q{RegexpCrawler is a Ruby library for crawl data from website using regular expression.}
11
+ s.email = %q{flyerhzm@gmail.com}
12
+ s.extra_rdoc_files = [
13
+ "LICENSE",
14
+ "README"
15
+ ]
16
+ s.files = [
17
+ ".gitignore",
18
+ "LICENSE",
19
+ "README",
20
+ "Rakefile",
21
+ "TODO",
22
+ "VERSION",
23
+ "init.rb",
24
+ "lib/regexp_crawler.rb",
25
+ "lib/regexp_crawler/crawler.rb",
26
+ "regexp_crawler.gemspec",
27
+ "spec/regexp_crawler_spec.rb",
28
+ "spec/resources/complex.html",
29
+ "spec/resources/nested1.html",
30
+ "spec/resources/nested2.html",
31
+ "spec/resources/simple.html",
32
+ "spec/spec.opts",
33
+ "spec/spec_helper.rb"
34
+ ]
35
+ s.homepage = %q{}
36
+ s.rdoc_options = ["--charset=UTF-8"]
37
+ s.require_paths = ["lib"]
38
+ s.rubygems_version = %q{1.3.5}
39
+ s.summary = %q{RegexpCrawler is a Ruby library for crawl data from website using regular expression.}
40
+ s.test_files = [
41
+ "spec/spec_helper.rb",
42
+ "spec/regexp_crawler_spec.rb"
43
+ ]
44
+
45
+ if s.respond_to? :specification_version then
46
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
47
+ s.specification_version = 3
48
+
49
+ if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
50
+ else
51
+ end
52
+ else
53
+ end
54
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: flyerhzm-regexp_crawler
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Richard Huang
@@ -32,6 +32,7 @@ files:
32
32
  - init.rb
33
33
  - lib/regexp_crawler.rb
34
34
  - lib/regexp_crawler/crawler.rb
35
+ - regexp_crawler.gemspec
35
36
  - spec/regexp_crawler_spec.rb
36
37
  - spec/resources/complex.html
37
38
  - spec/resources/nested1.html