웹 페이지에서 텍스트를 읽고 싶습니다. 나는 웹 페이지의 HTML 코드를 얻고 싶지 않다. 이 코드는 다음과 같습니다.자바로 웹 페이지에서 텍스트를 읽는 방법은 무엇입니까?
try {
// Create a URL for the desired page
URL url = new URL("http://www.uefa.com/uefa/aboutuefa/organisation/congress/news/newsid=1772321.html#uefa+moving+with+tide+history");
// Read all the text returned by the server
BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));
String str;
while ((str = in.readLine()) != null) {
str = in.readLine().toString();
System.out.println(str);
// str is one line of text; readLine() strips the newline character(s)
}
in.close();
} catch (MalformedURLException e) {
} catch (IOException e) {
}
이 코드는 웹 페이지의 HTML 코드를 제공합니다. 나는이 페이지 안에 전체 텍스트를 가져 가고 싶다. Java로 어떻게 할 수 있습니까?
HTML 태그의 텍스트를 구문 분석하면됩니다. 거기에서 원하는 정보를 찾고 거기에서 추출 할 수 있습니다. –
DOM을위한 HTML을 찾고 있다면 http://stackoverflow.com/questions/457684/reading-html-file-to-dom-tree-using-java가 도움이 될 것입니다. –
FYI - 반복마다 in.readLine()을 두 번 호출하므로 실제로는 모든 홀수 라인을 건너 뜁니다. (Java로 웹 페이지를 읽을 때 Google 검색의 첫 번째 결과 중 하나이므로이 코드의 버그를 지적해야한다고 생각했습니다.) – JPProgrammer