Class: IndeedScraper

Inherits:
Object
  • Object
show all
Defined in:
lib/indeedscraper.rb

Instance Method Summary collapse

Constructor Details

#initialize(searchterm, location) ⇒ IndeedScraper

Returns a new instance of IndeedScraper.



8
9
10
11
12
# File 'lib/indeedscraper.rb', line 8

def initialize(searchterm, location)
  @searchterm = searchterm
  @location = location
  @output = Array.new
end

Instance Method Details

#dateCheck(date) ⇒ Object

Handle year only dates



158
159
160
161
162
163
164
# File 'lib/indeedscraper.rb', line 158

def dateCheck(date)
  if date.length == 4
    return "January " + date
  else
    return date
  end
end

#dateParse(date) ⇒ Object

Process dates



135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
# File 'lib/indeedscraper.rb', line 135

def dateParse(date)
  datearray = Array.new
  daterange = date.text.split(" to ")
  if daterange[0] != nil
    datearray[0] = DateTime.parse(dateCheck(daterange[0]))
  else
    datearray[0] = nil
  end
  
  if daterange[1] == "Present"
    datearray[1] = "Present"
  else
    if daterange[1] != nil
      datearray[1] = DateTime.parse(dateCheck(daterange[1]))
    else
      datearray = nil
    end
  end

  return datearray
end

#getOutputObject

Generates JSON output



223
224
225
# File 'lib/indeedscraper.rb', line 223

def getOutput
  JSON.pretty_generate(@output)
end

#getResume(url) ⇒ Object

Process and save resume data



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/indeedscraper.rb', line 49

def getResume(url)
  page = Nokogiri::HTML(open(url))
  name = page.css('h1[itemprop="name"]').text
  location = page.css('p.locality').text
  currtitle = page.css('h2[itemprop="jobTitle"]').text
  summary = page.css('p#res_summary').text
  additionalinfo = page.css('div#additionalinfo-section').text
  skills = page.css("div#skills-section").text

  # Get work info
  page.css("div.work-experience-section").each do |w|
    positionhash = Hash.new
    positionhash[:name] = name
    positionhash[:url] = url
    positionhash[:title] = w.css("p.work_title").text
    if w.css("div.work_company").css("span")[0]
      positionhash[:company] = w.css("div.work_company").css("span")[0].text
    end
    if w.css("div.work_company").css("span")[1]
      positionhash[:company_location] = w.css("div.work_company").css("span")[1].text
    end

    # Process date info
    dates = dateParse(w.css("p.work_dates"))
    if dates
      positionhash[:start_date] = dates[0]
      positionhash[:end_date] = dates[1]
    end

    positionhash[:description] =  w.css("p.work_description").text

    # Info for all positions
    positionhash[:skills] = skills
    positionhash[:current_location] = location
    positionhash[:current_title] = currtitle
    positionhash[:summary] = summary
    positionhash[:additional_info] = additionalinfo
    @output.push(positionhash)
  end

  # Get education info
  page.css("div.education-section").each do |e|
    eduhash = Hash.new
    eduhash[:name] = name
    eduhash[:url] = url
    eduhash[:degree] = e.css("p.edu_title").text
    eduhash[:school] = e.css('span[itemprop="name"]').text
    eduhash[:dates] = e.css("p.edu_dates").text

    # Info for all degrees
    eduhash[:skills] = skills
    eduhash[:current_location] = location
    eduhash[:current_title] = currtitle
    eduhash[:summary] = summary
    eduhash[:additional_info] = additionalinfo
    @output.push(eduhash)
  end

  # Get military service info
  page.css("div.military-section").each do |m|
    milhash = Hash.new
    milhash[:name] = name
    milhash[:url] = url
    milhash[:service_country] = m.css("p.military_country").text.gsub("Service Country: ", "") 
    milhash[:branch] = m.css("p.military_branch").text.gsub("Branch: ", "")
    milhash[:rank] = m.css("p.military_rank").text.gsub("Rank: ", "")
  
    # Parse dates                                                                                                     
    dates = dateParse(m.css("p.military_date"))
    milhash[:start_date] = dates[0]
    milhash[:end_date] = dates[1]

    milhash[:military_description] = m.css("p.military_description").text
    milhash[:military_commendations] = m.css("p.military_commendations").text.split("\n")

    # Info for all items
    milhash[:skills] = skills
    milhash[:current_location] = location
    milhash[:current_title] = currtitle
    milhash[:summary] = summary
    milhash[:additional_info] = additionalinfo
    @output.push(milhash)
  end
end

#searchJobsObject

Search for jobs



167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
# File 'lib/indeedscraper.rb', line 167

def searchJobs
  @searchterm.gsub!(" ", "+")
  if @location != nil
    @location.gsub!(", ", "%2C+")
    @location.gsub!(" ", "+")
    url = "http://www.indeed.com/jobs?q=" + @searchterm + "&l=" + @location
  else
    url = "http://www.indeed.com/jobs?q=" + @searchterm + "&l="
  end
  html = Nokogiri::HTML(open(url))

  # Handle multiple pages
  numresults = html.css("div#searchCount").text.split(" of ")
  fresult = numresults[1].to_i/10.0
  if fresult != numresults[1].to_i/10
    count = fresult +1
  else
    count = numresults[1].to_i/10
  end

  # Loop through pages and get results
  i = 1
  while i <= count
    # Parse each listing
    html.css("div.row").each do |r|
      jobhash = Hash.new
      jobhash[:position] = r.css("h2.jobtitle").text.strip.lstrip
      jobhash[:company] = r.css("span.company").text.strip.lstrip
      jobhash[:location] = r.css('span[itemprop="jobLocation"]').text.strip.lstrip
      if r.css("h2.jobtitle").css("a")[0]
        jobhash[:url] = "http://indeed.com" + r.css("h2.jobtitle").css("a")[0]["href"]
        begin
          jobhash[:text] = Nokogiri::HTML(open(jobhash[:url])).text
        rescue
          begin
            jobhash[:text] = Nokogiri::HTML(open(jobhash[:url], :allow_redirections => :all)).text
          rescue
          end
        end
      end
      @output.push(jobhash)
    end

    # Get next page
    i += 1
    nextstart = (i-1)*10
    if @location != nil
      url = "http://www.indeed.com/jobs?q=" + @searchterm + "&l=" + @location + "&start=" + nextstart.to_s
    else
      url = "http://www.indeed.com/jobs?q=" + @searchterm + "&start=" + nextstart.to_s
    end
    html = Nokogiri::HTML(open(url))
  end
end

#searchResumesObject

Get all results



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/indeedscraper.rb', line 15

def searchResumes
  @searchterm.gsub!(" ", "-")
  if @location != nil
    @location.gsub!(", ", "-")
    @location.gsub!(" ", "-")
    url = "http://www.indeed.com/resumes/" + @searchterm + "/in-" + @location
  else
    url = "http://www.indeed.com/resumes/" + @searchterm
  end
  html = Nokogiri::HTML(open(url))
  
  # Handle multiple pages
  numresults = html.css("div#result_count").text.split(" ")
  fresult = numresults[0].to_i/50.0
  if fresult != numresults[0].to_i/50
    count = fresult +1
  else
    count = numresults[0].to_i/50
  end
  
  # Loop through pages and get results
  i = 1
  while i <= count
    results = html.css("ol#results")
    results.css("li").each do |l|
      getResume("http://indeed.com"+l.css("a")[0]["href"].gsub("?sp=0",""))
    end
    i += 1
    nextstart = (i-1)*50
    html = Nokogiri::HTML(open(url+"?start="+nextstart.to_s))
  end
end