Class: IndeedScraper2022
- Inherits:
-
Object
- Object
- IndeedScraper2022
- Defined in:
- lib/indeed_scraper2022.rb
Direct Known Subclasses
Instance Attribute Summary collapse
-
#browser ⇒ Object
readonly
Returns the value of attribute browser.
Instance Method Summary collapse
-
#initialize(url = 'https://uk.indeed.com/?r=us', q: '', location: '', headless: true, cookies: nil, debug: false) ⇒ IndeedScraper2022
constructor
A new instance of IndeedScraper2022.
- #page(n) ⇒ Object
-
#results ⇒ Object
returns an array containing the job search result.
- #search(q: @q, location: @location, start: nil) ⇒ Object
Constructor Details
#initialize(url = 'https://uk.indeed.com/?r=us', q: '', location: '', headless: true, cookies: nil, debug: false) ⇒ IndeedScraper2022
Returns a new instance of IndeedScraper2022.
29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/indeed_scraper2022.rb', line 29 def initialize(url='https://uk.indeed.com/?r=us', q: '', location: '', headless: true, cookies: nil, debug: false) @debug = debug @url_base, @q, @location = url, q, location @headless, = headless, fw = FerrumWizard.new( headless: @headless, cookies: , debug: @debug) @browser = fw.browser end |
Instance Attribute Details
#browser ⇒ Object (readonly)
Returns the value of attribute browser.
27 28 29 |
# File 'lib/indeed_scraper2022.rb', line 27 def browser @browser end |
Instance Method Details
#page(n) ⇒ Object
166 167 168 169 170 171 172 173 174 |
# File 'lib/indeed_scraper2022.rb', line 166 def page(n) if n < 1 or n > @results.length then raise IndeedScraper2022Err, 'Invalid page no.' end url = @results[n-1][:link] fetchjob(url) end |
#results ⇒ Object
returns an array containing the job search result
43 44 45 |
# File 'lib/indeed_scraper2022.rb', line 43 def results() @results end |
#search(q: @q, location: @location, start: nil) ⇒ Object
47 48 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 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 |
# File 'lib/indeed_scraper2022.rb', line 47 def search(q: @q, location: @location, start: nil) puts 'inside search' if @debug url = @url_base url += 'start=' + start if start puts 'url: ' + url.inspect if @debug @browser.goto(url) #@browser.network.wait_for_idle puts 'sleeping for 4 seconds' if @debug sleep 4 if q.length > 1 then input = @browser.at_xpath("//input[@name='q']") # select any existing text and overwrite it input.focus.type(:home); sleep 0.2 input.focus.type(:shift, :end); sleep 0.2 input.focus.type(q); sleep 0.2 end if location.length > 1 then input2 = @browser.at_xpath("//input[@name='l']") # select any existing text and overwrite it input2.focus.type(:home); sleep 0.2 input2.focus.type(:shift, :end); sleep 0.2 input2.focus.type(location); sleep 0.2 end = @browser.at_xpath("//button[@type='submit']") .click #@browser.network.wait_for_idle puts 'sleeping for 2 seconds' if @debug sleep 2 doc2 = Nokogiri::XML(@browser.body) File.write '/tmp/body.txt', doc2.to_s if @debug a2 = doc2.root.xpath "//li/div[div/div/div/div/table/tbody/tr/td/div/h2/a]" puts 'a2: ' + a2.length.inspect if @debug @a2 = a2.map {|x| Rexle.new x.to_s } @results = @a2.map do |doc| div = doc.element("div[@class='cardOutline']/div[@class='slider" \ "_container']/div[@class='slider_list']/div[@class='sl" \ "ider_item']/div[@class='job_seen_beacon']") td = div.element("table[@class='jobCard_mainContent']/tbo" \ "dy/tr/td[@class='resultContent']") # job title (e.g. Software Developer) job = td.element("div[@class='tapItem-gutter']/h2[@" \ "class='jobTitle-color-purple']/a") href = job.attributes[:href] jobtitle = job.element("span")&.text puts 'jobtitle: ' + jobtitle.inspect if @debug sal = td.element("div[@class='metadataContainer']/" \ "div[@class='salary-snippet-container']") salary = if sal then sal_e = sal.element("div[@class='attribute_snippet']") if sal_e then sal_e.texts[0] else sal_e2 = sal.element("div[@class='salary-snippet']/span") sal_e2 ? sal_e2.text : '' end else '' end puts 'salary: ' + salary.inspect if @debug div1 = td.element("div[@class='companyInfo']") # company name (e.g. Coda Octopus Products Ltd) coname = div1.element("span[@class='companyName']") puts 'coname: ' + coname.text.inspect if @debug company_name = coname.text.to_s.strip.length > 1 ? coname.text : coname.element('a').text # company location (e.g. Edinburgh) location = div1.element("div[@class='companyLocation']")&.text tbody = div.element("table[@class='jobCardShelfContainer']/tbody") div3 = tbody.element("tr[@class='underShelfFooter']/td/di" \ "v[@class='result-footer']") # job (e.g. Our products are primarily written in C#, using...) advert_items = div3.xpath("div[@class='job-snippet']/ul/li/text()") jobsnippet = if advert_items.any? then advert_items.join("\n") else div3.element("div[@class='job-snippet']").text end # visually (e.g. Posted 14 days ago) dateposted = div3.element("span[@class='date']")&.texts date = (Date.today - dateposted.first.to_i).to_s if dateposted { link: @url_base.sub(/\/[^\/]+$/,'') \ + href.gsub(/&/,'&'), title: jobtitle, salary: salary, company: company_name, location: location, jobsnippet: jobsnippet, date: date } end end |