0

シンプルなリンクチェッカー アプリケーションを構築しようとしています。ウェブページからすべての href 属性を抽出し、ファイルに出力します。次に、正規表現に対して解析した内容をチェックして、有効な URL をチェックし、有効な URL を別のファイルに出力します。次に、それらの URL にアクセスし、壊れたリンクを 3 番目のファイルに出力します。

以下の要約されたコードでは、href がすでに抽出され、page_contents.txt にリストされていると想定されています。そのテキスト ファイルの内容をここに示します。

http://computing.dcu.ie/~humphrys/
http://computing.dcu.ie/~humphrys/
http://computing.dcu.ie/~humphrys/blog.html
http://computing.dcu.ie/~humphrys/teaching.html
http://computing.dcu.ie/~humphrys/research.html
http://computing.dcu.ie/~humphrys/contact.html
http://computing.dcu.ie/~humphrys/
http://computing.dcu.ie/~humphrys/ca249/
http://computing.dcu.ie/~humphrys/ca318/
http://computing.dcu.ie/~humphrys/ca425/
http://computing.dcu.ie/~humphrys/ca651/
http://w2mind.computing.dcu.ie/
http://w2mind.org/
index.html
computers.internet.html
#world
#ireland
#uk
#multimedia
#internet
http://www.pressreader.com/
http://www.pressdisplay.com/
http://www.newspaperdirect.com/
http://www.newseum.org/todaysfrontpages/
http://news.google.com/
http://news.google.com/news?ned=uk
http://news.google.com/news?ned=en_ie
http://www.google.com/alerts
http://en.wikinews.org/
http://news.yahoo.com/
http://uk.news.yahoo.com/
http://www.apimages.com/
http://en.wikipedia.org/wiki/Next_Media_Animation
http://www.youtube.com/user/NMAWorldEdition
http://www.youtube.com/user/NMANews
http://www.time.com/
http://www.newsweek.com/
http://www.economist.com/
http://www.salon.com/
http://www.tnr.com/
http://thenewrepublic.com/
http://www.nytimes.com/
http://www.nypost.com/
http://www.washingtonpost.com/
http://www.latimes.com/
http://www.wsj.com/
http://www.jpost.com/
http://www.smh.com.au/
http://www.theonion.com/
http://www.theonion.com/content/video
http://www.youtube.com/user/TheOnion
http://www.theonion.com/content/radionews
http://www.thedailymash.co.uk/
http://themire.net/
http://waterfordwhispersnews.com/
http://www.evilgerald.com/
http://www.langerland.com/
http://www.portadownnews.com/
http://www.portadownnews.com/archive.htm
http://www.irishurls.com/
http://www.irishtimes.com/
http://www.irish-times.com/
http://www.ireland.com/
http://notices.irishtimes.com/
http://www.irishtimes.com/search/
http://www.independent.ie/
http://www.unison.ie/irish_independent/
http://www.independent.ie/search/index.jsp
http://www.announcement.ie/
http://www.iannounce.co.uk/Republic-of-Ireland/52
http://www.sbpost.ie/
http://www.thepost.ie/
http://archives.tcm.ie/businesspost/
http://en.wikipedia.org/wiki/Sunday_Tribune
http://www.irishexaminer.com/
http://www.examiner.ie/
http://www.magill.ie/
http://www.villagemagazine.ie/
http://www.phoenix-magazine.com/
http://www.hotpress.com/
http://www.emigrant.ie/
http://groups.google.com/groups/dir?sel=gtype%3D0%2Cusenet%3Die&
http://www.listenlive.eu/ireland.html
http://www.rte.ie/
http://www.rte.ie/player/
http://www.rte.ie/tv/
http://www.rte.ie/news/
http://www.rte.ie/aertel/170-01.html
http://www.rte.ie/radio/
http://www.rte.ie/radio1/
http://www.rte.ie/smiltest/radio_new.smil
http://www.rte.ie/lyricfm/
http://dynamic.rte.ie/av/live/radio/lyric.smil
http://www.rte.ie/aertel/184-01.html
http://www.tv3.ie/
http://www.tg4.ie/
http://www.tnag.ie/
http://www.rte.ie/aertel/
http://www.rte.ie/aertel/103-01.html
http://www.irishtimes.com/weather/
http://www.rte.ie/weather/
http://dir.yahoo.com/Regional/Countries/United_Kingdom/News_and_Media/
http://www.thetimes.co.uk/
http://www.the-times.co.uk/
http://www.timesonline.co.uk/
http://en.wikipedia.org/wiki/The_Times
http://www.thesundaytimes.co.uk/
http://www.sunday-times.co.uk/
http://archive.timesonline.co.uk/tol/archive/
http://www.thetimes.co.uk/tto/archive/
http://www.newsint-archive.co.uk/
http://www.newstext.com.au/
http://www.telegraph.co.uk/
http://www.independent.co.uk/
http://www.guardian.co.uk/
http://en.wikipedia.org/wiki/The_Guardian
http://www.observer.co.uk/
http://observer.guardian.co.uk/
http://archive.guardian.co.uk/
http://browse.guardian.co.uk/
http://www.guardian.co.uk/Archive/
http://users.guardian.co.uk/help/search/
http://www.spectator.co.uk/
http://www.private-eye.co.uk/
http://www.newstatesman.co.uk/

いくつかの異なるページを使用して問題なくプログラムを実行しましたが、特定の 1 つのページで次のエラー メッセージが表示されます。

Exception in thread "main" java.lang.IllegalArgumentException: protocol = http host = null
    at sun.net.spi.DefaultProxySelector.select(Unknown Source)
    at sun.net.www.protocol.http.HttpURLConnection.plainConnect(Unknown Source)
    at sun.net.www.protocol.http.HttpURLConnection.connect(Unknown Source)
    at sun.net.www.protocol.http.HttpURLConnection.followRedirect(Unknown Source)
    at sun.net.www.protocol.http.HttpURLConnection.getInputStream(Unknown Source)
    at sun.net.www.protocol.http.HttpURLConnection.getHeaderFieldKey(Unknown Source)
    at test2.main(test2.java:77)

エラーはコードのこの行にあります

String name = http.getHeaderFieldKey(i);

このトピックに関する以前の質問への回答は、プログラムが URL のホストを null として読み取っていることに問題があることを示唆しています。なぜこれが当てはまるのかわかりません(ホストがnullであることが問題の根本であると仮定しますか?)。問題の原因となっている URL はhttp://www.newstatesman.co.uk/であり、整形式であり、プログラムによって正しく処理される他の多くの URL と何ら変わりはないようです。

これは多かれ少なかれ私の最初の質問なので、問題または私の質問の定式化に関する建設的なコメントは大歓迎です。

import javax.swing.text.html.*;
import javax.swing.text.Element;
import javax.swing.text.ElementIterator;
import javax.swing.text.SimpleAttributeSet;
import javax.swing.text.BadLocationException;
import java.net.*;
import java.io.*;
import java.util.regex.*;

class test2 
{
    public static void main (String args[]) throws Exception
    {
        String fileOut2 = System.getProperty("user.dir") + File.separator + "page_contents.txt";
        String fileURLOut = System.getProperty("user.dir") + File.separator + "urls.txt";
        String brokenLinks = System.getProperty("user.dir") + File.separator + "broken2.html";


        BufferedReader URLIn = new BufferedReader(new FileReader(fileOut2));
        PrintWriter URLOut = new PrintWriter(new FileWriter(fileURLOut));
        PrintWriter brokenOut = new PrintWriter(new FileWriter(brokenLinks));



        try
        {


            String urlPattern = "((https?|ftp|gopher|telnet):((//)|(\\\\))+[\\w\\d:#@%/;$()~_?\\+-=\\\\\\.&]*)";



            String x;

            while ((x = URLIn.readLine()) != null)
            {
                System.out.println("Entered while loop!");
                Pattern p =     Pattern.compile(urlPattern,Pattern.CASE_INSENSITIVE);

                Matcher m = p.matcher(x);

                if (m.find())               
                {
                    URLOut.println(x.substring(m.start(0),m.end(0)));  


                    URL url = new URL(x.substring(m.start(0),m.end(0)));
                    HttpURLConnection http = (HttpURLConnection)url.openConnection();
                    http.setConnectTimeout(5000);
                    for (int i=0; ; i++) 
                    {
                        String name = http.getHeaderFieldKey(i);
                        String value = http.getHeaderField(i);

                        if (name == null && value == null)     // end of headers
                        {
                            break;         
                        }

                        if (name == null)     // first line of headers
                        {
                            if(!value.substring(9, 12).equals("200"))
                            {
                                brokenOut.println("<li><a href=\"" + url + "\">" + url + "</a>" + " " + value.substring(9, 12) + "</li>");
                            }
                        }
                        else
                        {
                            System.out.println(name + "=" + value + "!!!!!!");
                        }
                    }
                }
            }   

        } catch (MalformedURLException e) 
        {
            System.out.println("Malformed URL!!!!!");
        } catch (IOException e) 
        {
            throw new RuntimeException("IO Exception!!!!!", e);
        } finally
        {
            if (URLIn != null)
            {
                URLIn.close();
            }
            if (URLOut != null)
            {
                URLOut.close();
            }
            if (brokenOut != null)
            {
                brokenOut.close();
            }
        }
    }   
}  
4

1 に答える 1

0

ループ内で例外を処理したい場合、不正な URL から回復する機会が得られます。

于 2014-04-22T21:44:14.807 に答える