Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
B
Basic_Registration_form
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Thi Thanh Yen Hoang
Basic_Registration_form
Commits
56eb2218
Commit
56eb2218
authored
Jul 01, 2016
by
Thi Thanh Yen Hoang
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
first commit
parent
201e696b
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
90 additions
and
0 deletions
+90
-0
crawled.rb
+90
-0
No files found.
crawled.rb
0 → 100644
View file @
56eb2218
require
'nokogiri'
require
'open-uri'
require
'csv'
class
Crawler
def
initialize
# link queue(to be crawled)
@queue
=
[
"http://kenh14.vn"
]
# crawled links
@crawled
=
[]
@data
=
file
#@test = 0
#@test2 = ""
end
def
run
# while there still have link in queue
while
link
=
next_link
# get HTML and parse using the usual Nokogiri gem
p
link
page
=
Nokogiri
::
HTML
(
open
(
link
))
# analyze page content
page_analyze
(
page
,
link
)
# push new link to queue
push_link
(
page
)
content
(
page
)
#if @test > 0
#p @test
#p @test2
#break
#end
end
end
def
page_analyze
(
doc
,
link
)
@title
=
doc
.
css
(
"title"
).
text
end
def
push_link
(
doc
)
@url
=
doc
.
css
(
"link[rel=canonical] @href"
).
text
doc
.
css
(
"a @href"
).
each
do
|
url
|
if
url
.
value
.
include?
(
"http://"
)
&&
url
.
value
.
include?
(
".chn"
)
&&
!
@crawled
.
include?
(
url
.
value
)
&&
!
url
.
value
.
include?
(
".php"
)
&&
!
url
.
value
.
include?
(
"/video"
)
@queue
<<
url
.
value
@crawled
<<
url
.
value
#if url.value == "http://kenh14.vn//trang-2.chn"
#@test = 1
#end
end
if
!
url
.
value
.
include?
(
"http://kenh14.vn"
)
&&
url
.
value
.
include?
(
".chn"
)
&&
!
@crawled
.
include?
(
"http://kenh14.vn"
+
url
.
value
)
&&
!
url
.
value
.
include?
(
".php"
)
&&
!
url
.
value
.
include?
(
"/video"
)
&&
!
url
.
value
.
include?
(
"//trang-"
)
val
=
"http://kenh14.vn"
+
url
.
value
@queue
<<
val
@crawled
<<
val
#if val == "http://kenh14.vn//trang-2.chn"
#@test2 = url.value
#@test = 2
#end
end
end
end
def
next_link
link
=
nil
link
=
@queue
.
shift
return
link
end
def
content
(
doc
)
@content1
=
doc
.
css
(
"h2[class=knd-sapo]"
).
text
@content2
=
doc
.
css
(
"div[class=knd-content]"
).
text
@data
<<
[
@url
,
@title
,
@content1
+
@content2
]
end
def
file
csv
=
CSV
.
open
(
"test.csv"
,
"wb"
)
csv
<<
[
"URL"
,
"TITLE"
,
"CONTENT"
]
#csv << [@url, @title, @content1 + @content2]
return
csv
end
end
a
=
Crawler
.
new
a
.
run
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment