Commit e529714f by Ngo Trung Hung

crawler cities data

parent 0fda4287
...@@ -2,5 +2,10 @@ class HomeController < ApplicationController ...@@ -2,5 +2,10 @@ class HomeController < ApplicationController
def index def index
craw_data_companies() craw_data_companies()
# val = "\r\n \r\n\tĐược thành lập vào năm 2002, Công ty Cổ phần Maison (Maison) đã nhanh chóng trở thành một trong những công ty phân phối thời trang lớn nhất trong việc giới thiệu các thương hiệu thời trang cao cấp và sang trọng tại Việt Nam. Sau thành công ngoài mong đợi của cửa hàng đầu tiên, hiện Maison đang là ngôi nhà chung của hơn 23 thương hiệu đình đám thế giới.\r\n\r\n\t \r\n\r\n\tNhân viên là tài sản giá trị nhất của chúng tôi bởi vai trò quan trọng họ mang lại trong quá trình đạt được mục tiêu kinh doanh của Maison. Chúng tôi luôn hỗ trợ phát triển kỹ năng và chuyên môn ở từng giai đoạn sự nghiệp của mọi người.\r\n\r\n\tLuôn có những cơ hội mở để phát triển bản thân và sự nghiệp, bởi chúng tôi hiểu rằng con người là tài sản cốt lõi để củng cố doanh nghiệp và danh tiếng thương hiệu.\r\n\r\n\tChúng tôi \"will be ahihi\" tin rằng áp dụng những giá trị này vào thực tế sẽ tạo nền tảng lợi ích lâu dài đối với con người.\r\n\r\n\t \r\n\r\n "
# val.to_s.delete!("[\n,\t,\r]")
# val.lstrip!
# val.rstrip!
# @data = val
end end
end end
...@@ -26,32 +26,56 @@ module CrawlerHelper ...@@ -26,32 +26,56 @@ module CrawlerHelper
def crawl_link_for_companies_data def crawl_link_for_companies_data
@website = [] @website = []
num_page_will_crawl = 2 num_page_will_crawl = 1
agent = Mechanize.new agent = Mechanize.new
for i in 1..num_page_will_crawl do for i in 1..num_page_will_crawl do
page = agent.get("https://careerbuilder.vn/viec-lam/tat-ca-viec-lam-trang-#{i}-vi.html") page = agent.get("https://careerbuilder.vn/viec-lam/tat-ca-viec-lam-trang-#{i}-vi.html")
@website << page.search(".figcaption .caption a/@href").text.to_s.split('html') @website << page.search(".figcaption .caption a/@href").text.to_s.split('https://careerbuilder.vn/')
end end
for x in 0..(num_page_will_crawl - 1) do
for y in 0..@website[x].length do
@website[x][y].to_s << 'html'
end
end
@website = @website.join(",") @website = @website.join(",")
@website = @website.split(",") @website = @website.split(",").uniq!
@website @website = @website.select { |val| val != ''}
end end
def craw_data_companies def craw_data_companies
link_crawl = crawl_link_for_companies_data() link_crawl = crawl_link_for_companies_data()
link_crawl.uniq! @data_companies = {}
@data_companies_name = []
@data_companies_address = []
@data_companies_description = []
agent = Mechanize.new agent = Mechanize.new
link_crawl.each do |url| link_crawl.each do |url|
# debugger page = agent.get("https://careerbuilder.vn/#{url}")
page = agent.get("#{url.to_s}") if page.search(".company-info .info .content .name").text == ""
name = page.search(".section-page #cp_company_name").text
@data_companies_name << name.to_s.rstrip
address = page.search(".section-page .cp_basic_info_details ul li:nth-child(1)").text
@data_companies_address << address.to_s.rstrip
desc = page.search(".cp_aboutus_item .content_fck").text
@data_companies_description << desc
else
name = page.search(".company-info .info .content .name").text
@data_companies_name << name.to_s.rstrip
address = page.search(".company-info .info .content p:nth-child(3)").text
@data_companies_address << address.to_s.rstrip
desc = page.search(".main-about-us .content").text
@data_companies_description << desc
end
end end
render plain: link_crawl @data_companies[:name] = @data_companies_name.select { |val| val != ''}
@data_companies[:address] = @data_companies_address.select { |val| val != ''}
@data_companies_description.each do |val|
val.to_s.delete!("[\n,\t,\r]")
val.lstrip!
val.rstrip!
end
@data_companies[:description] = @data_companies_description.select { |val| val != ''}
# render plain: "#{@data_companies[:name].length} -- #{@data_companies[:address].length} -- #{@data_companies[:description].length}"
render plain: @data_companies
end end
end end
\ No newline at end of file
...@@ -14,7 +14,6 @@ ...@@ -14,7 +14,6 @@
<%= render 'layouts/header' %> <%= render 'layouts/header' %>
<%= render 'layouts/padding' %> <%= render 'layouts/padding' %>
<%= yield %> <%= yield %>
jbesdjlqwdn.qw
<div class = "xxx"></div> <div class = "xxx"></div>
<%= render 'layouts/footer' %> <%= render 'layouts/footer' %>
</div> </div>
......
...@@ -14,7 +14,7 @@ default: &default ...@@ -14,7 +14,7 @@ default: &default
encoding: utf8 encoding: utf8
pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %> pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
username: root username: root
password: '1' password: '12345678'
socket: /var/run/mysqld/mysqld.sock socket: /var/run/mysqld/mysqld.sock
......
...@@ -41,6 +41,71 @@ namespace :db do ...@@ -41,6 +41,71 @@ namespace :db do
end end
end end
# # CRAWLER COMPANIES
task make_companies: :environment do
def crawl_link_for_companies_data
@website = []
num_page_will_crawl = 1
agent = Mechanize.new
for i in 1..num_page_will_crawl do
page = agent.get("https://careerbuilder.vn/viec-lam/tat-ca-viec-lam-trang-#{i}-vi.html")
@website << page.search(".figcaption .caption a/@href").text.to_s.split('https://careerbuilder.vn/')
end
@website = @website.join(",")
@website = @website.split(",").uniq!
@website = @website.select { |val| val != ''}
end
def craw_data_companies
link_crawl = crawl_link_for_companies_data()
@data_companies = {}
@data_companies_name = []
@data_companies_address = []
@data_companies_description = []
agent = Mechanize.new
link_crawl.each do |url|
page = agent.get("https://careerbuilder.vn/#{url}")
if page.search(".company-info .info .content .name").text == ""
name = page.search(".section-page #cp_company_name").text
@data_companies_name << name.to_s.rstrip
address = page.search(".section-page .cp_basic_info_details ul li:nth-child(1)").text
@data_companies_address << address.to_s.rstrip
desc = page.search(".cp_aboutus_item .content_fck").text
@data_companies_description << desc
else
name = page.search(".company-info .info .content .name").text
@data_companies_name << name.to_s.rstrip
address = page.search(".company-info .info .content p:nth-child(3)").text
@data_companies_address << address.to_s.rstrip
desc = page.search(".main-about-us .content").text
@data_companies_description << desc
end
end
@data_companies[:name] = @data_companies_name.select { |val| val != ''}
@data_companies[:address] = @data_companies_address.select { |val| val != ''}
@data_companies_description.each do |val|
val.to_s.delete!("[\n,\t,\r]")
val.lstrip!
val.rstrip!
end
@data_companies[:description] = @data_companies_description.select { |val| val != ''}
@data_companies
end
@data = craw_data_companies()
i = @data[:name].length
i.times do |n|
name = @data[:name][n]
address = @data[:address][n]
short_description = @data[:description][n]
password = "password"
Company.create!(name: name,
address: address,
short_description: short_description)
end
end
end end
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment