Created
December 10, 2016 18:24
-
-
Save felinebabies/f42ffe8398b67eff48f914beef65a934 to your computer and use it in GitHub Desktop.
猫の里親募集一覧の写真を取得するスクリプト
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| require "nokogiri" | |
| require "open-uri" | |
| require "erb" | |
| require "base64" | |
| TARGETURL = "http://www.city.yokohama.lg.jp/kenko/hokenjo/genre/douai/joto/jotoinfo-cat.html" | |
| dochtml = open(TARGETURL, "r:CP932").read | |
| doc = Nokogiri::HTML.parse(dochtml) | |
| # 日本語出力時には明示的にUTF-8に変換する | |
| # puts doc.css("table img").first.to_s.encode("UTF-8") | |
| catsimages = doc.css("table img").map do |item| | |
| URI.join(TARGETURL, item.attr("src")) | |
| end | |
| template = <<'EOS' | |
| <html> | |
| <head> | |
| <meta charset="UTF-8"> | |
| <title>譲渡先募集猫写真</title> | |
| </head> | |
| <body> | |
| <h1>譲渡相手募集中の猫写真一覧</h1> | |
| <% catsimages.each do |imgurl| %> | |
| <p> | |
| <img src="<%= 'data:image/jpg;base64,' + Base64.strict_encode64(open(imgurl).read) %>" alt="猫写真"/> | |
| </p> | |
| <% end %> | |
| </body> | |
| </html> | |
| EOS | |
| result = ERB.new(template).result(binding) | |
| puts result |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment