SSISO Community

시소당

html페이지로부터 url 링크 뽑기

이  소스를  사용할수  있도록  처리좀  해주세요  이소스를  실행하면  아래와  같은  에러가  나옵니다  ㅠ-ㅠ  도와주세요

  

----------  javac  ----------
123.java:27:  class  EnumerateURLLink  is  public,  should  be  declared  in  a  file  named  EnumerateURLLink.java
public  class  EnumerateURLLink
              ^
123.java:59:  cannot  resolve  symbol
symbol    :  variable  url
location:  class  EnumerateURLLink
                  URLConnection  conn  =  url.openConnection();
                                                            ^
2  errors

출력  완료  (0초  경과)  -  정상  종료

  

--------------------------------------------------------------------------------------------------------

아래는  제가  사용한  소스입니다.

  

  

import  java.io.InputStreamReader;

import  java.io.Reader;

import  java.net.HttpURLConnection;

import  java.net.URL;

import  java.net.URLConnection;

  

import  javax.swing.text.Document;

import  javax.swing.text.EditorKit;

import  javax.swing.text.ElementIterator;

import  javax.swing.text.SimpleAttributeSet;

import  javax.swing.text.html.HTML;

import  javax.swing.text.html.HTMLEditorKit;

  

public  class  EnumerateURLLink

{

      public  static  void  main(String[]  args)

      {

            HttpURLConnection.setFollowRedirects(false);

            EditorKit  kit  =  new  HTMLEditorKit();

            Document  doc  =  kit.createDefaultDocument();

  

            //  The  Document  class  does  not  yet  handle

//  charset's  properly.

            doc.putProperty("IgnoreCharsetDirective",  Boolean.TRUE);

  

            try

            {

                  //  Create  a  reader  on  the  HTML  content.

                  URL  url_  =  new  URL("http://www.google.co.kr");

                  URLConnection  conn  =  url.openConnection();

  

                  Reader  rd

=  new  InputStreamReader(conn.getInputStream());

  

                  //  Parse  the  HTML.

                  kit.read(rd,  doc,  0);

  

                  //  Iterate  through  the  elements  of  the  HTML  document.

                  ElementIterator  it  =  new  ElementIterator(doc);

                  javax.swing.text.Element  elem;

                        

                  while((elem  =  it.next())  !=  null)

                  {

                        SimpleAttributeSet  s  =  (SimpleAttributeSet)

                        elem.getAttributes().getAttribute(HTML.Tag.A);

                            

                        if  (s  !=  null)

                        {

                              System.out.println("URL  Link  =  "

+  s.getAttribute(HTML.Attribute.HREF));

                        }

                  }

            }

            catch  (Exception  e)

            {

                  e.printStackTrace();

            }

      }

}

--------------------------------------------------------------------------------------------------------
소스파일  이름을  123.java  라고  하셨군요.

자바의  소스파일명은  소스에  public인  클래스가  있을  때

public인  클래스  이름과  똑같게  해주어야  합니다.

그래서  123.java인  파일이름을  EnumerateURLLink.java  라고  수정해  주어야  합니다.

  

두번째  에러는  url  이라는  것이  존재하지  않는다는  말이고

그  위쪽에  url로  판단되는  URL  url_    <--를  선언하고  있는데

이것을  URL  url  로  수정하던지  사용하는  url을  url_  로  수정하면  되겠습니다.

  

                  URL  url_  =  new  URL("http://www.google.co.kr");
                  URLConnection  conn  =  url.openConnection();

=>

  

                  URL  url  =  new  URL("http://www.google.co.kr");
                  URLConnection  conn  =  url.openConnection();

  

or

  

  

                  URL  url_  =  new  URL("http://www.google.co.kr");
                  URLConnection  conn  =  url.openConnection();

=>

  

                  URL  url_  =  new  URL("http://www.google.co.kr");
                  URLConnection  conn  =  url_.openConnection();

출처  :  http://kin.naver.com/detail/detail.php?d1id=1&dir_id=10105&eid=5uc0slt9Ep2Tq253/1vmVUZ9bifprg1J
&qb=bmV3IFVSTCg=

1516 view

4.0 stars