Skip to content

Instantly share code, notes, and snippets.

@felinebabies
Created December 10, 2016 18:24
Show Gist options
  • Select an option

  • Save felinebabies/f42ffe8398b67eff48f914beef65a934 to your computer and use it in GitHub Desktop.

Select an option

Save felinebabies/f42ffe8398b67eff48f914beef65a934 to your computer and use it in GitHub Desktop.
猫の里親募集一覧の写真を取得するスクリプト
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