前提・実現したいこと
お世話になります。
あるwebサイトを、地域によってURLを変えながらRubyでMechanizeを用いてスクレイピングしたいと考えています。each文によってURLを変えていこうと考えたのですが、下記のようなコードではエラーが発生してしまいました。
rb
1def url(current_page) 2 area = ["hokkaido?page=","aomori?page=","iwate?page=","miyagi?page=","akita?page=","yamagata?page=","fukushima?page=","ibaraki?page=","tochigi?page=","gunma?page=","saitama?page=","chiba?page=","tokyo?page=","kanagawa?page=","niigata?page=","yamanashi?page=","nagano?page=","toyama?page=","ishikawa?page=","fukui?page=","gifu?page=","shizuoka?page=","aichi?page=","mie?page=","shiga?page=","kyoto?page=","osaka?page=","hyogo?page=","nara?page=","wakayama?page=","tottori?page=","shimane?page=","okayama?page=","hiroshima?page=","yamaguchi?page=","tokushima?page=","kagawa?page=","ehime?page=","kochi?page=","fukuoka?page=","saga?page=","nagasaki?page=","kumamoto?page=","oita?page=","miyazaki?page=","kagoshima?page=","okinawa?page="] 3 area.each do |a| 4 "https://example/search/#{a}#{current_page}" 5 end 6end 7 8 9name = [] 10 11(1..10).each do|i| 12 page = agent.get(url(i)) 13 page.search('.name').each do |n| 14 name << n 15 end 16 17 sleep 1 18end
そこで問題を切り分けようと、
rb
1def a 2 area = ["hokkaido?page=","aomori?page=","iwate?page=","miyagi?page=","akita?page=","yamagata?page=","fukushima?page=","ibaraki?page=","tochigi?page=","gunma?page=","saitama?page=","chiba?page=","tokyo?page=","kanagawa?page=","niigata?page=","yamanashi?page=","nagano?page=","toyama?page=","ishikawa?page=","fukui?page=","gifu?page=","shizuoka?page=","aichi?page=","mie?page=","shiga?page=","kyoto?page=","osaka?page=","hyogo?page=","nara?page=","wakayama?page=","tottori?page=","shimane?page=","okayama?page=","hiroshima?page=","yamaguchi?page=","tokushima?page=","kagawa?page=","ehime?page=","kochi?page=","fukuoka?page=","saga?page=","nagasaki?page=","kumamoto?page=","oita?page=","miyazaki?page=","kagoshima?page=","okinawa?page="] 3 area.each do |a| 4 "https://example/search/#{a}" 5 end 6end
としてコードを切り出し、ターミナルでputs aを実行してみたところ、"https://example/search/"が追加されず、"~~?page="の部分だけが出力されてしまいました。理由がわからず、下記のように、メソッドの中でputsを書いてメソッドを実行したところ、思い通りの結果が出力されました。
この理由がわかりません。
なぜ上記のコードでは"https://example/search/"が追加されず、下記のコードでは追加されるのでしょうか?
rb
1def a 2 area = ["hokkaido?page=","aomori?page=","iwate?page=","miyagi?page=","akita?page=","yamagata?page=","fukushima?page=","ibaraki?page=","tochigi?page=","gunma?page=","saitama?page=","chiba?page=","tokyo?page=","kanagawa?page=","niigata?page=","yamanashi?page=","nagano?page=","toyama?page=","ishikawa?page=","fukui?page=","gifu?page=","shizuoka?page=","aichi?page=","mie?page=","shiga?page=","kyoto?page=","osaka?page=","hyogo?page=","nara?page=","wakayama?page=","tottori?page=","shimane?page=","okayama?page=","hiroshima?page=","yamaguchi?page=","tokushima?page=","kagawa?page=","ehime?page=","kochi?page=","fukuoka?page=","saga?page=","nagasaki?page=","kumamoto?page=","oita?page=","miyazaki?page=","kagoshima?page=","okinawa?page="] 3 area.each do |a| 4 puts "https://example/search/#{a}" 5 end 6end
発生している問題・エラーメッセージ
absolute URL needed (not %5B%22hokkaido?page=%22,%20%22aomori?page=%22,%20%22iwate?page=%22,%20%22miyagi?page=%22,%20%22akita?page=%22,%20%22yamagata?page=%22,%20%22fukushima?page=%22,%20%22ibaraki?page=%22,%20%22tochigi?page=%22,%20%22gunma?page=%22,%20%22saitama?page=%22,%20%22chiba?page=%22,%20%22tokyo?page=%22,%20%22kanagawa?page=%22,%20%22niigata?page=%22,%20%22yamanashi?page=%22,%20%22nagano?page=%22,%20%22toyama?page=%22,%20%22ishikawa?page=%22,%20%22fukui?page=%22,%20%22gifu?page=%22,%20%22shizuoka?page=%22,%20%22aichi?page=%22,%20%22mie?page=%22,%20%22shiga?page=%22,%20%22kyoto?page=%22,%20%22osaka?page=%22,%20%22hyogo?page=%22,%20%22nara?page=%22,%20%22wakayama?page=%22,%20%22tottori?page=%22,%20%22shimane?page=%22,%20%22okayama?page=%22,%20%22hiroshima?page=%22,%20%22yamaguchi?page=%22,%20%22tokushima?page=%22,%20%22kagawa?page=%22,%20%22ehime?page=%22,%20%22kochi?page=%22,%20%22fukuoka?page=%22,%20%22saga?page=%22,%20%22nagasaki?page=%22,%20%22kumamoto?page=%22,%20%22oita?page=%22,%20%22miyazaki?page=%22,%20%22kagoshima?page=%22,%20%22okinawa?page=%22%5D) (ArgumentError)
該当のソースコード
rb
1def url(current_page) 2 area = ["hokkaido?page=","aomori?page=","iwate?page=","miyagi?page=","akita?page=","yamagata?page=","fukushima?page=","ibaraki?page=","tochigi?page=","gunma?page=","saitama?page=","chiba?page=","tokyo?page=","kanagawa?page=","niigata?page=","yamanashi?page=","nagano?page=","toyama?page=","ishikawa?page=","fukui?page=","gifu?page=","shizuoka?page=","aichi?page=","mie?page=","shiga?page=","kyoto?page=","osaka?page=","hyogo?page=","nara?page=","wakayama?page=","tottori?page=","shimane?page=","okayama?page=","hiroshima?page=","yamaguchi?page=","tokushima?page=","kagawa?page=","ehime?page=","kochi?page=","fukuoka?page=","saga?page=","nagasaki?page=","kumamoto?page=","oita?page=","miyazaki?page=","kagoshima?page=","okinawa?page="] 3 area.each do |a| 4 "https://example/search/#{a}#{current_page}" 5 end 6end 7 8 9name = [] 10 11 12(1..2).each do|i| 13 page = agent.get(url(i)) 14 page.search('.name').each do |n| 15 name << n 16 end 17 18 sleep 1 19end
補足情報(FW/ツールのバージョンなど)
ruby 2.6.3p62 (2019-04-16 revision 67580) [x86_64-linux]
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2020/06/08 05:10
2020/06/08 08:07
2020/06/08 11:32