실제 다운로드 받은 문서가 redirect된다면 프로그램으로 다운로드 받은 페이지는 실제 우리가 브라우저에서 보는 내용과 다를 수 있다.
http://a.com으로 접근하면 서버는 respose를 주는데 내용은 아래에서 볼 수 있다.
예를 들어, http://photohito.com/photo/261862 라는 사이트에 들어가면 서버는 301이라는 결과를 준다.
- Request URL:http://photohito.com/photo/261862
- Request Method:GET
- Status Code:301 Moved Permanently
브라우져는 301을 redirect하라는 것으로 이해하기 때문에 redirect할 주소를 찾아 http://photohito.com/photo/261862/로 다시 요청을 하게 된다.
현재 예는 url 마지막의 /이 있고 없고의 차이지만 redirect의 형태와 방법에는 여러가지가 있다.
wiki의 url redirection 페이지이다.
http://en.wikipedia.org/wiki/URL_redirection
어떤 이유로 redirect되는지 확인해보자
http://en.wikipedia.org/wiki/URL_redirection#HTTP_status_codes_3xx
In the HTTP protocol used by the World Wide Web, a redirect is a response with a status code beginning with 3 that induces a browser to go to another location, with annotation describing the reason, which allows for the correct subsequent action (such as changing links in the case of code 301, a permanent change of address)
The HTTP standard defines several status codes for redirection:
- 300 multiple choices (e.g. offer different languages)
- 301 moved permanently
- 302 found (originally temporary redirect, but now commonly used to specify redirection for unspecified reason)
- 303 see other (e.g. for results of cgi-scripts)
- 307 temporary redirect
All of these status codes require that the URL of the redirect target be given in the Location: header of the HTTP response. The 300 multiple choices will usually list all choices in the body of the message and show the default choice in the Location: header.
Within the 3xx range, there are also some status codes that are quite different from the above redirects (they are not discussed here with their details):
- 304 not modified
- 305 use proxy
그럼 어떤 방식으로 redirect 할 수 있는지도 보자
첫번째로 header에 포함되는 경우이다.
respose로 우리는 header와 본문을 받게 되는데
HTTP/1.1 301 Moved Permanently Location: http://www.example.org/ Content-Type: text/html Content-Length: 174 <html> <head> <title>Moved</title> </head> <body> <h1>Moved</h1> <p>This page has moved to <a href="http://www.example.org/">http://www.example.org/</a>.</p> </body> </html>
브라우저에서는 301을 보고 리다이렉트라 판단한 후 Location: http://www.example.org/ 로 이동하게 된다.
두번째는 html의 meta tag를 이용하는 것이다.
<html> <head> <meta http-equiv="Refresh" content="0; url=http://www.example.com/" /> </head> <body> <p>Please follow <a href="http://www.example.com/">this link</a>.</p> </body> </html>
위와 같이 작성된 페이지는 브라우저가 확인하여 redirect시켜 준다.
같은 동작을 header에 정의 할 수도 있다.
HTTP/1.1 200 ok Refresh: 0; url=http://www.example.com/ Content-type: text/html Content-length: 78 Please follow <a href="http://www.example.com/">this link</a>!
세번째는 자바스크립트로 redirect하는 방법이다.
이건 위키에 예가 없어서 대충 찾음 http://www.tizag.com/javascriptT/javascriptredirect.php
<script type="text/javascript"> <!-- window.location = "http://www.google.com/" //--> </script>
네번째는 프레임을 사용하는 방법
<frameset rows="100%"> <frame src="http://www.example.com/"> </frameset> <noframes> <body>Please follow <a href="http://www.example.com/">link</a>!</body> </noframes>
설명이 괜찮았다면 광고를 한번 눌러주는 것이다!!!
'Crawling' 카테고리의 다른 글
크롤러 (crawler) (0) | 2011.12.08 |
---|---|
크롤링에서 문서의 최신성 (freshness) (0) | 2011.09.06 |
URL 파싱하기 (0) | 2011.08.09 |
url에 program으로 접근되지 않을 때 (0) | 2011.07.05 |
수집시에 HTTP 헤더의 if modified since (0) | 2011.06.24 |