HTMLで言うと、例えば
<a href="http://q.hatena.ne.jp/">はてな</a>
ならば、
「http://q.hatena.ne.jp/」
と
「はてな」
を取得してくれると嬉しいです。
WebページのURLを入力したら、該当ページ内にある全ての「リンク先のURLと文字列」をドーッっと取得してくれるのが理想です。調べたいWebページは数百あるので、できるだけ力作業にならないようにしたいです。
フリーソフトやプログラムでも構いませんが、上記が叶う「使いやすい!」という方法等ありましたら、どうぞよろしくお願い致します。
Ruby で書いてみました。
require 'net/http' require 'uri' Net::HTTP.version_1_1 #RE_A = %r|<a\s[^>]+href="([^"]+)"[^>]*>([^<]+)</a>|i TransFreeBSD さんの突っ込みを受けて RE_A = %r|<a\s[^>]*href="([^"]+)"[^>]*>(.+?)</a>|i def extract_anchor(url) response = http_get(url,10) body = response.body index = 0 last = body.length - 1 while (p = (RE_A =~ body[index, last])) puts "#{$1} --> #{$2}" index += p + $1.length + $2.length end end $http_clazz = Net::HTTP def http_get( uri_str, limit = 10 ) raise ArgumentError, 'http redirect too deep' if limit == 0 uri = URI.parse(uri_str) response = nil target = $http_clazz.new(uri.host, uri.port) target.read_timeout = 600 target.start { |http| if uri.query.nil? then response, = http.get(uri.path) else response, = http.get(uri.path + '?' + uri.query) end } return response end target_url = "http://q.hatena.ne.jp/1334564001" extract_anchor(target_url)
抽出対象のページを、この質問の URL にしてみました。
結果は、こちら。
https://www.hatena.ne.jp/register?location=http%3A%2F%2Fq.hatena.ne.jp%2F1334564001 --> ユーザー登録 https://www.hatena.ne.jp/login?backurl=http%3A%2F%2Fq.hatena.ne.jp%2F1334564001 --> ログイン /help --> ヘルプ / --> トップ /list --> 質問一覧 /ranking --> 注目の質問 /c/korenani --> コレナニ? /enter --> 質問する /enter?type=enquete --> アンケートする / --> 人力検索はてな http://q.hatena.ne.jp/"> --> http://q.hatena.ne.jp/"> http://q.hatena.ne.jp/ --> http://q.hatena.ne.jp/ #c236156 --> 2012/04/16 19:13:08 https://www.hatena.ne.jp/login?backurl=http%3A%2F%2Fq.hatena.ne.jp%2F1334564001 --> すでに登録されている方はこちらからログイン
最後の
target_url = "http://q.hatena.ne.jp/1334564001" extract_anchor(target_url)
の部分を、
while gets extract_anchor($_.chomp) end
とすると、URL の一覧を別ファイルに持っておいて、それを順次処理するようにできます。
言語は何でも良いと思いますが、何か一つ覚えておくと、こういうときに便利です。
もし、Ruby 以外をお望みなら「○○のコードが欲しいー」と、大きい声で叫ぶと、あんな人や、こんな人(どんな人?)が、コードを書いてくれそうな気がします。
とりあえず、Perl や PHP だったら、もう試している人がいるような気がします。
/ --> <img class="service-logo" src="/images/logo_question.png?new" alt="人力検索はてな" title="人力検索はてな"> https://www.hatena.ne.jp/register?location=http%3A%2F%2Fq.hatena.ne.jp%2F1334564001 --> ユーザー登録 https://www.hatena.ne.jp/login?backurl=http%3A%2F%2Fq.hatena.ne.jp%2F1334564001 --> ログイン /help --> ヘルプ http://www.hatena.ne.jp/ --> <img src="http://cdn.www.st-hatena.com/images/header/global-logo.png" alt="Hatena"> / --> トップ /list --> 質問一覧 /ranking --> 注目の質問 /c/korenani --> コレナニ? /enter --> 質問する /enter?type=enquete --> アンケートする / --> 人力検索はてな /c/computer --> コンピュータ /miku1973/ --> <img src="http://www.st-hatena.com/users/mi/miku1973/profile_l.gif" width="32" height="32" title="id:miku1973" alt="id:miku1973" class="hatena-id-image"> /miku1973/ --> miku1973 /miku1973/ --> もっと見る /1334564001 --> <img src="/images/icon-jinriki-s.gif" alt="" title="" class="question-type"> /c/computer --> コンピュータ /c/internet --> インターネット http://q.hatena.ne.jp/"> --> http://q.hatena.ne.jp/"> http://q.hatena.ne.jp/ --> http://q.hatena.ne.jp/ https://www.hatena.ne.jp/login?backurl=http%3A%2F%2Fq.hatena.ne.jp%2F1334564001 --> <img src="/images/button-login-hatena.gif" alt="はてなIDでログイン" title="はてなIDでログイン"> ${url} --> <img src="${image_url}" alt="${alt}" data-urlname="${urlname}" class="hatena-id-image">{{if exuser}}<img src="/images/q/border-${exservice}.gif" class="badge-service">{{/if}}<img src="${emoticon_url}" alt="${emoticon_alt}" class="emoticonicon"> /a-kuma3/ --> <img src="http://www.st-hatena.com/users/a-/a-kuma3/profile_l.gif" width="32" height="32" title="id:a-kuma3" alt="id:a-kuma3" class="hatena-id-image"> /a-kuma3/ --> a-kuma3 /1334564001#a1142689 --> 2012/04/16 21:32:39 https://www.hatena.ne.jp/register?location=http%3A%2F%2Fq.hatena.ne.jp%2F1334564001 --> https://www.hatena.ne.jp/register?location=http%3A%2F%2Fq.hatena.ne.jp%2F1334564001 https://www.hatena.ne.jp/login?backurl=http%3A%2F%2Fq.hatena.ne.jp%2F1334564001 --> https://www.hatena.ne.jp/login?backurl=http%3A%2F%2Fq.hatena.ne.jp%2F1334564001 http://q.hatena.ne.jp/"> --> http://q.hatena.ne.jp/"> http://q.hatena.ne.jp/"> --> http://q.hatena.ne.jp/"> http://q.hatena.ne.jp/ --> http://q.hatena.ne.jp/ http://q.hatena.ne.jp/ --> http://q.hatena.ne.jp/ https://www.hatena.ne.jp/login?backurl=http%3A%2F%2Fq.hatena.ne.jp%2F1334564001 --> https://www.hatena.ne.jp/login?backurl=http%3A%2F%2Fq.hatena.ne.jp%2F1334564001 /maya70828/ --> <img src="http://www.st-hatena.com/users/ma/maya70828/profile_l.gif" width="32" height="32" title="id:maya70828" alt="id:maya70828" class="hatena-id-image"> /maya70828/ --> 楽1978 /1334564001#a1142690 --> 2012/04/16 21:36:21 http://www.forest.impress.co.jp/article/2005/06/17/ablink.html --> http://www.forest.impress.co.jp/article/2005/06/17/ablink.html http://www.vector.co.jp/soft/dl/win95/net/se261717.html --> http://www.vector.co.jp/soft/dl/win95/net/se261717.html http://inforati.jp/apple/mac-tips-techniques/windows-hints/how-to-install-virtualbox-and-use-windows-software-in-mac.html --> http://inforati.jp/apple/mac-tips-techniques/windows-hints/how-to-install-virtualbox-and-use-windows-software-in-mac.html https://www.virtualbox.org/wiki/Downloads --> https://www.virtualbox.org/wiki/Downloads http://wayohoo.com/mac/apps/utilities/mikuinstaller.html --> http://wayohoo.com/mac/apps/utilities/mikuinstaller.html /language_and_engineering/ --> <img src="http://www.st-hatena.com/users/la/language_and_engineering/profile_l.gif" width="32" height="32" title="id:language_and_engineering" alt="id:language_and_engineering" class="hatena-id-image"> /language_and_engineering/ --> lang_and_engine /1334564001#a1142745 --> 2012/04/17 10:41:36 /TransFreeBSD/ --> <img src="http://www.st-hatena.com/users/Tr/TransFreeBSD/profile_l.gif" width="32" height="32" title="id:TransFreeBSD" alt="id:TransFreeBSD" class="hatena-id-image"> /TransFreeBSD/ --> <img src="http://www.st-hatena.com/users/Tr/TransFreeBSD/profile_s.gif" width="16" height="16" alt="" class="hatena-id-icon">TransFreeBSD #c236156 --> 2012/04/16 19:13:08 http://twitter.com/#!/${screen_name}/status/${id} --> <img src="${profile_image_url}" class="profile-image" width="32" height="32"> http://twitter.com/#!/${screen_name}/status/${id} --> @${screen_name} /help/answer_request --> 制限について #ar-tabs-3 --> ともだちから選択 #ar-tabs-4 --> リクエスト送信済 http://rikunabi-next.yahoo.co.jp/ --> 転職ならリクナビNEXT http://b.hatena.ne.jp/entry/http://q.hatena.ne.jp/1334564001 --> <img src="http://b.st-hatena.com/images/entry-button/button-only.gif" alt="このエントリーをはてなブックマークに追加" width="20" height="20" style="border: none;" /> http://twitter.com/share --> ツイートする /TransFreeBSD/watchlist --> <img src="http://www.st-hatena.com/users/Tr/TransFreeBSD/profile.gif" width="24" height="24" title="id:TransFreeBSD" alt="id:TransFreeBSD" class="hatena-id-image"> /a-kuma3/watchlist --> <img src="http://www.st-hatena.com/users/a-/a-kuma3/profile.gif" width="24" height="24" title="id:a-kuma3" alt="id:a-kuma3" class="hatena-id-image"> /keyword/%E3%83%95%E3%83%AA%E3%83%BC%E3%82%BD%E3%83%95%E3%83%88 --> <span class="list-tag-word" data-score="47">フリーソフト</span><span class="related-count">1732</span> /keyword/%E3%81%AF%E3%81%A6%E3%81%AA --> <span class="list-tag-word" data-score="43">はてな</span><span class="related-count">4842</span> /keyword/HTML --> <span class="list-tag-word" data-score="34">HTML</span><span class="related-count">3994</span> /keyword/%E3%83%97%E3%83%AD%E3%82%B0%E3%83%A9%E3%83%A0 --> <span class="list-tag-word" data-score="23">プログラム</span><span class="related-count">4106</span> /keyword/URL --> <span class="list-tag-word" data-score="23">URL</span><span class="related-count">9396</span> /keyword/%E6%96%87%E5%AD%97%E5%88%97 --> <span class="list-tag-word" data-score="23">文字列</span><span class="related-count">1672</span> https://www.hatena.ne.jp/register?location=http%3A%2F%2Fq.hatena.ne.jp%2F1334564001&via=200101 --> <img src="/images/button-register.gif" alt="いますぐ利用登録!無料" /> https://www.hatena.ne.jp/login?backurl=http%3A%2F%2Fq.hatena.ne.jp%2F1334564001 --> すでに登録されている方はこちらからログイン /1237778469 --> <span class="question-content">★採用回答には 300 ~ 800 pt★ 以下に示す「ユーザーの動作」を私が行った場合に、「プログラム/スクリプトの動作」を自動で行ってくれる方法を教えて下さい…</span> /Nigitama/ --> にぎたま /1323393541 --> <span class="question-content">A8の楽天市場のアフィリエイトがリニューアルされてA8の管理画面からリンクを作るようになりましたが、以前のように商品の検索結果をアフィリエイトのリンクに…</span> /ringomaru/ --> ringomaru /1323393541#a1122008 --> <img src="/images/badge-bestanswer-s.gif" alt="ベストアンサーあり"> /1164276573 --> <span class="question-content">IISのASP(VBScript)でWiki記法のパーサーのようなものを実装したいとおもっています。…</span> /youichirou/ --> よーいち /1200474454 --> <span class="question-content">フリーソフトを探しています。自分で作る場合のヒントでも結構ですので情報をお願いします。…</span> /freewriter/ --> にらたま /1155130885 --> <span class="question-content">SSL、CGI を使ったサイトの質問です。 https://www.example.jp/form.html にアクセスしたときの form.html に張られている相対リンク(例えば、./help.html) をク…</span> /ishideo/ --> ishideo /1328175221 --> <span class="question-content">Ubuntu11.04でGVIMを利用していますが、 コマンドモード時に日本語入力ができなくて困っています。…</span> /hardboiled243/ --> hardboiled243 /1328926452 --> <span class="question-content">WEB上でテキストを入力すると、その内容に応じて情報を表示するシステムの作成方法について 以下のようなサイト内のシステムをどのように作ったらよいのか 質問…</span> /lilmo345/ --> lilmo345 /1328926452#a1132033 --> <img src="/images/badge-bestanswer-s.gif" alt="ベストアンサーあり"> ${url} --> <img src="${image_url}"> ${url} --> ${title} http://b.hatena.ne.jp/entry/${url} --> <img src="http://b.hatena.ne.jp/entry/image/${url}">
先の回答は、自戒の意味を込めて、元の形が残るように修正しました。
macやlinuxは使えますか?
perl/python/rubyあたりが使えると楽そうな気がします。
macならautomatorでもいけるかもしれません。
vba+ieでも出来ると思いますが、数が多いのでエラー処理とか安定性とか速度とか、若干不安です。
専用ツールがあれば一番楽でしょうけれど。