1 like 0 dislike
28 views
in Ruby by
ruby mechanize temel komutlar

1 Answer

0 like 0 dislike
by anonymous Bilge (165k points)
require 'mechanize'
require 'logger'

###################################################################################
# Mechanize tanımı yapılıyor
mechanize = Mechanize.new
mechanize.log = Logger.new "mech.log"

###################################################################################
### Sayfa alınıyor
begin
  page = mechanize.get "http://www.kutlubilgi.com/register?to="
rescue Mechanize::ResponseReadError => e
  page = e.force_parse
end
############################## NAVIGATION #########################################
# mechanize.get(uri)
# mechanize.post(uri, query, headers)
# mechanize.visited?(url)
# mechanize.click(link)
# mechanize.download(uri, filename)
# mechanize.resolve(link)
puts  mechanize.page.title
puts  mechanize.back.title

############################## PAGES ##############################################
# puts page.body
# puts page.title
# puts page.links
# puts page.forms
# puts page.images
# puts page.image_urls
# puts page.search('tr')                            # HTML etiketi yazılır a, h1, span, tr gibi
# puts page.at('link')                              # İlk bulduğu etikete gider
# puts page.form_with(dom_id: 'login-form')         # Form dom_id ile çağrılabilir
# puts page.form_with(action: './search')           # Form action adıyla kaydedilebilir
# puts page.link_with(text: 'Sorular')              # Linkler href, text gibi parametrelerle cağrılabilir
# puts page.link_with(href: /foo/)
# puts page.links_with(dom_class: /\bnav-link\b/)
# puts page.links_with(href: /\.pdf\z/)

###################################################################################
### Sayfa üzerindeki linkler basılıyor
# page.links.each do |link|
#   puts link.text
# end

###################################################################################
### Form ve ilgili form nesnesi aranıyor
# search_form = page.form_with(action: './search')
# puts search_form.has_field?('q').to_s
# search_form = page.select_form(nr=0)
# search_form.field_with(:name => "q").value = "ruby"

# search_results = mechanize.submit search_form
# puts search_results.body

###################################################################################
#puts "--- Mechanize Commands ---"
#puts mechanize.visited?("https://google.com")
#puts mechanize.visited?("http://www.kutlubilgi.com")
#puts mechanize.resolve("http://www.kutlubilgi.com")
# puts mechanize.page
# puts mechanize.current_page
# puts mechanize.visited_page("http://www.kutlubilgi.com")
...