자바로 빌보드 차트 웹사이트에서 정보읽어오기입니다.
조회수 730회
제목과 같이 자바로 파싱을 이용해서 빌보드차트 웹사이트의 정보를 긁어오는 코드인데요.
초보라서 잘은 모르지만 URL클래스를 이용해서 url객체를 생성하고 bufferedreader
를 통해 읽어오는것같은데 url클래스가 예외처리를 반드시 해야한다고 자바상에 떠서.. 결과값이 저는 예외처리를 했던 파싱에러가 출력이됩니다. 왜 읽어오지 못할까요?
package s89;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URL;
public class BillboardMain3 {
public static void main(String[] args) {
String newUrls="https://www.billboard.com/charts/hot-100/";
URL url=null;
try {
url = new URL(newUrls); //주소찾기
//주소지에 빨대 꽂기
BufferedReader reader = new BufferedReader(
new InputStreamReader(url.openStream(),"euc-kr"),8);
String line = null;
while((line = reader.readLine())!=null) { //한줄씩 읽어서
if(!line.trim().equals("")) { // 공백이 아니면 출력한다.
System.out.println(line.trim());
}
}
} catch (Exception e) {
System.out.println("Billboard Parsing error!!!");
}
}
}
2 답변
-
자바 200제 풀고 있으신가 보네요 저도 그거 하고 있습니당. ㅎㅎ
그거 billboard 차트사이트는 파싱을 막아 놓은것 같더라구요. 다른 사이트 도메인 치시면 잘 나와요.
코드를 조금만 손보면 그 뒤 부분도 잘 되는 편인듯 합니다. 아무래도 책을 낸지 좀 되서 그런 것 같더라구요.-
(•́ ✖ •̀)
알 수 없는 사용자
-
-
groovy 샘플입니다. 참고하세요.
groovy:000> @Grab(group='org.apache.httpcomponents', module='httpclient', version='4.4') groovy:001> go ===> null groovy:000> import org.apache.http.impl.client.* groovy:000> import org.apache.http.client.methods.* groovy:000> import org.apache.http.util.* groovy:000> httpClient = HttpClients.createDefault() groovy:000> httpGet = new HttpGet("https://www.billboard.com/charts/hot-100/") groovy:000> response = httpClient.execute(httpGet) ===> HttpResponseProxy{HTTP/1.1 200 OK [Date: Sat, 28 Sep 2019 17:51:51 GMT, Content-Type: text/html; charset=UTF-8, Transfer-Encoding: chunked, Connection: keep-alive, CF-Cache-Status: HIT, Cache-Control: max-age=1, public, s-maxage=300, CF-Ray: 51d7915b2b73a261-ICN, Age: 275, Expect-CT: max-age=604800, report-uri="https://report-uri.cloudflare.com/cdn-cgi/beacon/expect-ct", Last-Modified: Fri, 27 Sep 2019 19:50:29 GMT, Set-Cookie: PGMINFO=cc:kr-ip:116.37.93.4; Max-Age=3600; Path=/; Domain=.billboard.com, Vary: Accept-Encoding, Via: 1.1 varnish (Varnish/5.2), X-Cache-Hits: HIT (25), X-Debug-Cookies: , X-Debug-Log: Removed cookies, X-NX-Host: www.billboard.com, X-Varnish: 917770121 932119920, Server: cloudflare] org.apache.http.client.entity.DecompressingEntity@60d6fdd4} groovy:000> contents = EntityUtils.toString(response.getEntity(), "UTF-8") ===> <!doctype html> <html class="" lang=""> <head> <script> _udn = "billboard.com"; </script> <script>function utmx_section(){}function utmx(){}(function(){var k='67942495-39',d=document,l=d.location,c=d.cookie; if(l.search.indexOf('utm_expid='+k)>0)return; function f(n){if(c){var i=c.indexOf(n+'=');if(i>-1){var j=c. indexOf(';',i);return escape(c.substring(i+n.length+1,j<0?c. length:j))}}}var x=f('__utmx'),xx=f('__utmxx'),h=l.hash;d.write(
댓글 입력