質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.35%
Ruby

Rubyはプログラミング言語のひとつで、オープンソース、オブジェクト指向のプログラミング開発に対応しています。

Q&A

解決済

1回答

787閲覧

Rubyのメソッド内でputsを実行した場合と、メソッドを実行する際にputsをした場合の違い

tttkkm

総合スコア10

Ruby

Rubyはプログラミング言語のひとつで、オープンソース、オブジェクト指向のプログラミング開発に対応しています。

0グッド

1クリップ

投稿2020/06/07 00:32

編集2020/06/07 00:33

前提・実現したいこと

お世話になります。
ある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]

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

ベストアンサー

eachだからselfつまりarea = ["hokkaido?page=","aomori?page=",...]を返します

map(もしくはcollect)を使うと
["https://example/search/hokkaido?page="...]を返せます。

ただし、agent.getが配列に対応しているようには思いませんので

ruby

1urls = url(i) 2urls.each{|u| 3 page = agent.get(u)

的な事をしなければいけません。


なお、適切に待ち時間を入れずに連続でアクセスして逮捕されても私は責任を持ちません。

投稿2020/06/07 00:55

asm

総合スコア15149

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

tttkkm

2020/06/08 05:10

返答遅くなってしまい申し訳ありません!ありがとうございます!mapを使えばよかったんですね! わざわざ助言までありがとうございます、一応sleep 1を入れていたのですが、2にすることにしました! 最後にもしよろしければお聞かせ願いたいのですが、 urls = url(i) urls.each{|u| page = agent.get(u) こちら、 url(i).each{|u| page = agent.get(u) でも動作するのではないかと思うのですが、asmさんのように記述されるのにはそちらの方がわかりやすく、バグが発生しづらいから、とか何かしらの理由があるのでしょうか? ご教授いただければ幸いです。
asm

2020/06/08 08:07

特に意味はないですよ。 強いていえば、何かを何かに変換する系のメソッドは一度 p urls = url(i) 等で確認したい…という程度です。 それも大抵の場合は開発が進むにつれ後者のメソッドチェインに書き直す事も多いです
tttkkm

2020/06/08 11:32

ありがとうございます! なるほど、確認のためだったんですね、疑問が解消されました。重ね重ねありがとうございました!
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.35%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問