From d4d4cc97b3bb23117e533dbb8c43a4efd65e6f7d Mon Sep 17 00:00:00 2001 From: Kanezoh Date: Tue, 4 May 2021 09:56:02 +0900 Subject: [PATCH] add parser --- main.cr | 5 +++-- src/mechanize/page.cr | 8 ++++++++ 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/main.cr b/main.cr index fbfa943..2197a14 100644 --- a/main.cr +++ b/main.cr @@ -4,5 +4,6 @@ agent = Mechanize.new agent.request_headers = HTTP::Headers{"Foo" => "Bar"} params = {"hoge" => "hoge"} page = agent.get("http://example.com/", params: params) -puts page.code -puts page.body +#puts page.code +#puts page.body +puts page.parser.not_nil!.css("h1").first.inner_text diff --git a/src/mechanize/page.cr b/src/mechanize/page.cr index e9b56ef..72826c3 100644 --- a/src/mechanize/page.cr +++ b/src/mechanize/page.cr @@ -1,6 +1,14 @@ +require "myhtml" require "./file" + class MechanizeCr::Page < MechanizeCr::File def initialize(uri, response, body, code) super(uri, response, body, code) end + + def parser + return @parser if @parser + return unless @body + @parser = Myhtml::Parser.new(@body) + end end