Class: FormScraperHelper
- Inherits:
-
Object
- Object
- FormScraperHelper
- Defined in:
- lib/formscraper_helper.rb
Instance Attribute Summary collapse
-
#browser ⇒ Object
readonly
Returns the value of attribute browser.
Instance Method Summary collapse
-
#initialize(url = nil, browser: nil, headless: false, clipb: true, fd: nil, debug: false) ⇒ FormScraperHelper
constructor
note: fd corresponds to FakeDataGenerator22 which is optional.
- #scrape(body = @browser.body) ⇒ Object
- #to_code ⇒ Object
- #to_h ⇒ Object
-
#to_yaml ⇒ Object
creates a YAML document for the inputs.
Constructor Details
#initialize(url = nil, browser: nil, headless: false, clipb: true, fd: nil, debug: false) ⇒ FormScraperHelper
note: fd corresponds to FakeDataGenerator22 which is optional
16 17 18 19 20 21 22 23 |
# File 'lib/formscraper_helper.rb', line 16 def initialize(url=nil, browser: nil, headless: false, clipb: true, fd: nil, debug: false) @url, @clipb, @fd, @debug = url, clipb, fd, debug @browser = browser ? browser : FerrumWizard.new(url, headless: headless) end |
Instance Attribute Details
#browser ⇒ Object (readonly)
Returns the value of attribute browser.
12 13 14 |
# File 'lib/formscraper_helper.rb', line 12 def browser @browser end |
Instance Method Details
#scrape(body = @browser.body) ⇒ Object
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/formscraper_helper.rb', line 25 def scrape(body=@browser.body) puts 'body: ' + body.inspect if @debug doc = Nokorexi.new(body).to_doc #a = doc.root.xpath('//input|//select') a = doc.root.xpath('//*').select {|x| x.name == 'input' or x.name == 'select'} a.reject! do |x| x.attributes[:type] == 'hidden' or x.attributes[:style] =~ /display:none/ end @h = a.map do |x| key = x.attributes[:name] type = x.name h = {} h[:type] = x.attributes[:type] || type h[:xpath] = "//%s[@name=\"%s\"]" % [type, key] h[:title] = x.attributes[:title] if type == 'select' then h[:options] = x.xpath('option').map {|x| x.text.to_s} end [key, h] end.to_h end |
#to_code ⇒ Object
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 |
# File 'lib/formscraper_helper.rb', line 59 def to_code() s=<<EOF require 'yaml' require 'ferrum' require 'nokorexi' browser = Ferrum::Browser.new headless: false url = '#{@url}' browser.goto(url) sleep 2 doc = Nokorexi.new(browser.body).to_doc # load the YAML document containing the inputs #filepath = '' filepath = '/tmp/tmp.yaml' h = YAML.load(File.read(filepath)) EOF @h.each do |key, h| puts 'key: ' + key.inspect if @debug s += "r = browser.at_xpath('#{h[:xpath]}')\n" if h[:type] == 'text' or h[:type] == 'password' then var1, s2 = format_var1(h[:title], key) s += s2 s += var1 + " = h['#{var1}']\n" s += "r.focus.type #{var1}\n" s += "sleep 0.5\n\n" elsif h[:type] == 'select' var1, s2 = format_var1(h[:title], key) s += s2 s += "# options: #{h[:options].join(', ')}\n" s += "#{var1} = h['#{var1}']\n" s += 'titles = %w(' + h[:options].join(' ') + ')' + "\n" s += 'found = titles.grep /#{' + var1 + '}/i' + "\n" s += "n = titles.index(found.first) + 1\n" s += "r.focus\n" s += "n.times { r.type(:down); sleep 1}\n" s += "r.click\n" s += "sleep 0.5\n\n" elsif h[:type] == 'checkbox' s += "r.focus.click\n\n" end end Clipboard.copy s if @clipb puts 'generated code copied to clipboard' return s end |
#to_h ⇒ Object
55 56 57 |
# File 'lib/formscraper_helper.rb', line 55 def to_h() @h end |
#to_yaml ⇒ Object
creates a YAML document for the inputs
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 165 166 167 168 169 |
# File 'lib/formscraper_helper.rb', line 123 def to_yaml() s = '---' + "\n" @h.each do |key, h| puts 'key: ' + key.inspect if @debug if h[:type] == 'text' or h[:type] == 'password' then var1, s2 = format_var1(h[:title], key) s += s2 if h[:type] == 'password' then @pwd ||= @fd ? @fd.password : 'xxx' s += var1 + ": #{@pwd}\n" elsif @fd found = @fd.lookup var1 val = found.is_a?(String) ? found : 'xxx' s += var1 + ": '#{val}'\n" else s += var1 + ": xxx\n" end elsif h[:type] == 'select' var1, s2 = format_var1(h[:title], key) s += s2 s += "# options: #{h[:options].join(', ')}\n" val = h[:options][1..-1].sample s += "#{var1}: '#{val}'\n" elsif h[:type] == 'checkbox' end end Clipboard.copy s if @clipb puts 'generated YAML copied to clipboard' return s end |