Skip to content

javaScript:웹크롤링 질문입니다! #63

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
hyperpace opened this issue Jan 6, 2018 · 1 comment
Closed

javaScript:웹크롤링 질문입니다! #63

hyperpace opened this issue Jan 6, 2018 · 1 comment

Comments

@hyperpace
Copy link

제가 특정 사이트를 크롤링 하고 싶습니다.
그런데 cros 문제가 있더라구요.. 그래서 검색을 많이해보고 이해하려고 노력했지만
이해가 잘 안되서요.

  1. 도대체 브라우저에서 서버까지 어떤 요청과 응답이 있는건가요?
  2. 요청이나 응답을 받을 때 header값이 있던데 header의 역할은 무엇인가요?
  3. 클라이언트 측에서는 크롤링이 불가능한건가요?
(function() {
  var httpRequest;
  window.onload = function() { makeRequest('http://coinmarketcal.com/'); };
 
  function makeRequest(url) {
    if (window.XMLHttpRequest) { // Mozilla, Safari, ...
      httpRequest = new XMLHttpRequest();
    } else if (window.ActiveXObject) { // IE
      try {
        httpRequest = new ActiveXObject("Msxml2.XMLHTTP");
      } 
      catch (e) {
        try {
          httpRequest = new ActiveXObject("Microsoft.XMLHTTP");
        } 
        catch (e) {}
      }
    }
 
    if (!httpRequest) {
      alert('Giving up :( Cannot create an XMLHTTP instance');
      return false;
    }
    httpRequest.onreadystatechange = alertContents;
    console.log("Hello")

    httpRequest.open('GET', url);
    httpRequest.send();
  }
 
  function alertContents() {
    if (httpRequest.readyState === 4) {
      if (httpRequest.status === 200) {
        alert(httpRequest.responseText);
      } else {
        alert('There was a problem with the request.');
      }
    }
  }
})();
@davidkwak
Copy link
Collaborator

안녕하세요

1번과 2번 질문은 한번에 답변 드려야 될것 같습니다.

브라우저에서는 웹 요청을 서버로 보내고, 서버는 해당 요청에 따른 처리 결과를 응답으로 보냅니다.
이때 브라우저에서는 "request header"와 "request body (optional)" 을 보내는데 해당 헤더의 핵심 내용은 아래와 같습니다.

GET /data.zip HTTP/1.1 --> 어떤 method로 어떤 리소스를 요청할지
Host: www.server.com --> 대상 서버 주소
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) --> 브라우저 정보
Referer: https://www.google.co.kr/ --> 방금전에 있던 페이지 주소
Cookie: ssab=; webid=696fe0a0596811e790e4000af759d210; --> 접속자 쿠키 정보

위 요청을 받으면 서버에서는 해당 요청을 처리해서 data.zip 파일을 보내주게 됩니다.
(물론 권한이 없다거나, 서버 측의 정책에 따라 거부하거나 인증 페이지로 리다이렉트 하는 등의 행위가 가능합니다)

응답 측 header에서는 응답 코드 값 (200대 정상, 300대 리다에릭션, 400대 에러) 정도가 중요한 정보 입니다.
참고자료 : https://developer.mozilla.org/ko/docs/Web/HTTP/Status

마지막으로 3번째 질문의 답은 "크롤링은 원래 클라이언트에서 하는 것" 입니다.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants