open-uri
参考:
uri
参考:
使い方
参考:
open-uri による Web クローリング入門 | 株式会社ルーター
Ruby で Web 上からファイルをダウンロードする | UX MILK
open-uri で URL を開く方法
URI.parse
を使う
require "open-uri" URI.parse("https://example.com/").open do |f| // ... end
拡張された Kernel.#open
を使う
require "open-uri" open("https://example.com/") do |f| // ... end
参考:
instance method OpenURI::OpenRead#open (Ruby 2.6.0)
singleton method OpenURI.open_uri (Ruby 2.6.0)
Rails fetch url – Stack Overflow
open-uri で拡張された open で URL を開いた場合の戻り値
10 キロバイト(10,240 バイト)以下の場合は StringIO、それより大きい場合は Tempfile
参考:
Ruby の open_uri でファイルオブジェクトが Tempfile になる瞬間を追った – Qiita
Buffer – ruby/open-uri.rb at v2_6_0 · ruby/ruby – GitHub
Why does OpenURI treat files under 10kb in size as StringIO? – Stack Overflow
charset
require "open-uri" charset = nil html = open(url) do |f| charset = f.charset f.read end
参考:
instance method OpenURI::Meta#charset (Ruby 2.5.0)
Shift_JIS サイトを Nokogiri でパースするときにはエンコード指定 (CP932 指定推奨) – Qiita
open-uri で utf-8 以外のページを文字変換して取得する | deadwood
Nokogiri を使った Ruby スクレイピング | 酒と涙とRubyとRailsと
open-uri で UTF-8 でないページを取得するときの注意 | Route 477
charset のデフォルトを UTF-8 にする
open("http://example.com").tap do |io| charset = io.charset { "utf-8" } io.set_encoding(charset) end
参考:
Specify default charset using open-uri but use server-provided charset if given – Stack Overflow
base_uri
参考:
instance method OpenURI::Meta#base_uri (Ruby 2.5.0)
異なる scheme (HTTP/HTTPS) 間のリダイレクト
参考:
open-uri で http->https のリダイレクトが禁止されている件 – Qiita
OpenURI で http から https のリダイレクトを一時的に許す gem 作った | ぴよログ
open-uri で http=>https のリダイレクトを行う | 歩いたら休め
open-uri-redirections
参考:
Ruby open-uri redirect forbidden – Stack Overflow
BASIC 認証
参考:
User-Agent を指定する
URI.parse(url).open("User-Agent" => user_agent) do |f| content = f.read end
参考:
Ruby で RSS 取得するときに User-Agent を偽装する | 動かざることバグの如し
Ruby ではてなブックマークの RSS にアクセスすると503が返ってくる | /var/www/yatta47.log
How to set a custom user agent in ruby – Stack Overflow
URI
参考:
URI::Generic
参考:
class URI::Generic (Ruby 2.6.0)
URI.parse
参考:
singleton method URI.parse (Ruby 2.6.0)
OpenURI
参考:
OpenURI::Meta
サーバーから取得したデータの属性を扱うメソッドを追加する。OpenURI
を使用して開いた StringIO
が OpenURI::Meta
で extend
される。
参考:
module OpenURI::Meta (Ruby 2.6.0)
IO
参考:
instance method IO#read (Ruby 2.6.0)
File
参考:
Tempfile
参考:
StringIO
参考:
instance method StringIO#string (Ruby 2.6.0)
instance method StringIO#read (Ruby 2.6.0)
OpenURI::HTTPError
参考: