반응형

#web scraping

#web crawling

 

requests 모듈을 임포트한다

가장 먼저해야 할 일은 requests 모듈을 사용할 수 있도록 임포트하는 것입니다.

import requests

※전제조건

python이 설치되어 있어야 합니다.

pip install requests 등의 명령으로 모듈이 설치되어 있어야 합니다. 

 

get 메서드로 서버에 request 해서 response를 받아오기

url을 인자로 전달해서 서버에서 response(응답)를 받아 옵니다. 

이때 사용하는 메소드는 get 메서드입니다. 

 

requests.get(url)

 

url부분에 실제 url을 인자로 전달하면 됩니다. 여기서는 구글의 url을 사용해보겠습니다. 

response = requests.get('https://www.google.com')

 

response를 print(출력)하면 무엇이 나올까?

response를 출력해보겠습니다. 

print(response)

출력을 하면 반환되는 값은 아래와 같습니다. 

<Response [200]>

여기서 200이 의미하는 것은 요청이 정상 처리되었다는 것을 의미합니다. 200 등을 Response Code라고 하는데요. 전체적으로 이런 의미를 담고 있습니다. 

Response Code 의미 설명
1xx Informaitional 요청 받았으나 아직 처리 중
2xx Success 요청을 정상적으로 처리했음
3xx Redirection 요청을 완료하기 위해서 추가 동작이 필요 
4xx Client Error 클라이언트의 요청을 처리할 수 없어 오류 발생
5xx Server Error 서버에서 처리를 할 수 없어 오류 발생

 

response의 내용 출력하기

response(응답)의 내용은 아래와 같이 확인해볼 수 있습니다. 

 

response.content

 

내용을 출력합니다. 

print(response.content)

내용을 보면 아래와 같이 한 줄 형태로 된 html입니다. 구글의 사이트를 브라우저가 표시할 수 있도록 html을 response에 포함시켜 보내 준 것입니다. 

b'<!doctype html><html itemscope="" itemtype="http://schema.org/WebPage" lang="ja"><head><meta content="&#19990;&#30028;&#20013;&#12398;&#12354;&#12425;&#12422;&#12427;&#24773;&#22577;&#12434;&#26908;&#32034;&#12377;&#12427;&#12383;&#12417;&#12398;&#12484;&#12540;&#12523;&#12434;&#25552;&#20379;&#12375;&#12390;&#12356;&#12414;&#12377;&#12290;&#12373;&#12414;&#12374;&#12414;&#12394;&#26908;&#32034;&#27231;&#33021;&#12434;&#27963;&#29992;&#12375;&#12390;&#12289;&#12362;&#25506;&#12375;&#12398;&#24773;&#22577;&#12434;&#35211;&#12388;&#12369;&#12390;&#12367;&#12384;&#12373;&#12356;&#12290;" name="description"><meta content="noodp" name="robots"><meta content="text/html; charset=UTF-8" http-equiv="Content-Type"><meta content="/images/branding/googleg/1x/googleg_standard_color_128dp.png" itemprop="image"><title>Google</title><script nonce="dgHg/Go9ku6pd8Q253/V7A==">(function(){window.google={kEI:\'IQxuYcy0E5L2-Qb9uK_4CA\',kEXPI:\'0,1302536,56873,6059,206,4804,2316,383,246,5,1354,4013,1238,1122515,1197775,619,328873,51224,16114,28684,17572,4858,1362,283,9008,3022,2822,1930,12834,4020,978,13228,3847,3599,7023,19044,2780,917,5970,704,1279,2212,530,149,1943,6297,108,3406,606,2023,1777,520,14670,603,2624,1990,857,5,12354,5096,16320,908,2,3555,13142,3,576,6459,149,12289,1686,4,1528,2304,1236,5226,5261,2014,13611,2725,2039,2658,6701,656,30,5616,5797,2215,2307,637,1493,13406,3380,5815,2314,228,992,3102,3138,6,908,3,3541,1,14710,1814,283,38,874,5992,1352,14103,6,1267,1715,2,9839,36,4147,1244,687,1094,8,32,3161,5,3,4,1282,744,5852,1576,3,7294,1590,1160,1266,24,5409,301,19,2061,2719,3708,4,1478,4831,4635,3604,2,6,5,7749,2124,2445,2577,3136,542,3023,3,3609,91,834,2,4,2215,720,2,535,1853,5195,315,231,3752,348,690,1691,1734,1571,1629,1625,508,2688,98,1625,981,14,756,1701,1222,830,131,482,128,184,2,1025,273,129,419,2,529,55,21,67,219,1253,409,465,265,203,1113,42,108,38,3,2,211,276,325,85,637,402,39,745,211,118,4,186,96,431,311,336,125,3,291,736,285,382,61,304,2,1,3,182,552,162,4,50,294,115,110,2,2,1,473,647,164,1439,291,726,198,184,5554894,99,2,345,321,16,15,1802632,4193884,2800697,882,444,1,2,80,1,1796,1,9,2553,1,889,795,2,561,1,4265,1,1,2,1331,4142,2609,155,17,13,72,139,4,2,20,2,169,13,19,46,5,39,96,548,29,2,2,1,2,1,2,2,7,4,1,2,2,2,2,2,2,353,513,186,1,1,158,3,2,2,2,2,2,4,2,3,3,269,564,2,5,2,110,2,4,176,38,12,15,6,42,1,4,2,4,3,5,37,1,1,1,20733666,3220019,2770168,1271184,338,3,3422,483,9,1434,160,1356,285,2939,339,1724,686,812743\',kBL:\'Iapj\'};google.sn=\'webhp\';google.kHL=\'ja\';})();(function(){\nvar f=this||self;var h,k=[];function l(a){for(var b;a&&(!a.getAttribute||!(b=a.getAttribute("eid")));)a=a.parentNode;return b||h}function m(a){for(var b=null;a&&(!a.getAttribute||!(b=a.getAttribute("leid")));)a=a.parentNode;return b}\nfunction n(a,b,c,d,g){var e="";c||-1!==b.search("&ei=")||(e="&ei="+l(d),-1===b.search("&lei=")&&(d=m(d))&&(e+="&lei="+d));d="";!c&&f._cshid&&-1===b.search("&cshid=")&&"slh"!==a&&(d="&cshid="+f._cshid);c=c||"/"+(g||"gen_204")+"?atyp=i&ct="+a+"&cad="+b+e+"&zx="+Date.now()+d;/^http:/i.test(c)&&"https:"===window.location.protocol&&(google.ml&&google.ml(Error("a"),!1,{src:c,glmm:1}),c="");return c};h=google.kEI;google.getEI=l;google.getLEI=m;google.ml=function(){return null};google.log=function(a,b,c,d,g){if(c=n(a,b,c,d,g)){a=new Image;var e=k.length;k[e]=a;a.onerror=a.onload=a.onabort=function(){delete k[e]};a.src=c}};google.logUrl=n;}).call(this);(function(){\ngoogle.y={};google.sy=[];google.x=function(a,b){if(a)var c=a.id;else{do c=Math.random();while(google.y[c])}google.y[c]=[a,b];return!1};google.sx=function(a){google.sy.push(a)};google.lm=[];google.plm=function(a){google.lm.push.apply(google.lm,a)};google.lq=[];google.load=function(a,b,c){google.lq.push([[a],b,c])};google.loadAll=function(a,b){google.lq.push([a,b])};google.bx=!1;google.lx=function(){};}).call(this);google.f={};(function(){\ndocument.documentElement.addEventListener("submit",function(b){var a;if(a=b.target){var c=a.getAttribute("data-submitfalse");a="1"===c||"q"===c&&!a.elements.q.value?!0:!1}else a=!1;a&&(b.preventDefault(),b.stopPropagation())},!0);document.documentElement.addEventListener("click",function(b){var a;a:{for(a=b.target;a&&a!==document.documentElement;a=a.parentElement)if("A"===a.tagName){a="1"===a.getAttribute("data-nohref");break a}a=!1}a&&b.preventDefault()},!0);}).call(this);</script><style>#gbar,#guser{font-size:13px;padding-top:1px !important;}#gbar{height:22px}#guser{padding-bottom:7px !important;text-align:right}.gbh,.gbd{border-top:1px solid #c9d7f1;font-size:1px}.gbh{height:0;position:absolute;top:24px;width:100%}@media all{.gb1{height:22px;margin-right:.5em;vertical-align:top}#gbar{float:left}}a.gb1,a.gb4{text-decoration:underline !important}a.gb1,a.gb4{color:#00c !important}.gbi .gb4{color:#dd8e27 !important}.gbf .gb4{color:#900 !important}\n</style><style>body,td,a,p,.h{font-family:arial,sans-serif}body{margin:0;overflow-y:scroll}#gog{padding:3px 8px 0}td{line-height:.8em}.gac_m td{line-height:17px}form{margin-bottom:20px}.h{color:#1558d6}em{font-weight:bold;font-style:normal}.lst{height:25px;width:496px}.gsfi,.lst{font:18px arial,sans-serif}.gsfs{font:17px arial,sans-serif}.ds{display:inline-box;display:inline-block;margin:3px 0 4px;margin-left:4px}input{font-family:inherit}body{background:#fff;color:#000}a{color:#4b11a8;text-decoration:none}a:hover,a:active{text-decoration:underline}.fl a{color:#1558d6}a:visited{color:#4b11a8}.sblc{padding-top:5px}.sblc a{display:block;margin:2px 0;margin-left:13px;font-size:11px}.lsbb{background:#f8f9fa;border:solid 1px;border-color:#dadce0 #70757a #70757a #dadce0;height:30px}.lsbb{display:block}#WqQANb a{display:inline-block;margin:0 12px}.lsb{background:url(/images/nav_logo229.png) 0 -261px repeat-x;border:none;color:#000;cursor:pointer;height:30px;margin:0;outline:0;font:15px arial,sans-serif;vertical-align:top}.lsb:active{background:#dadce0}.lst:focus{outline:none}</style><script nonce="dgHg/Go9ku6pd8Q253/V7A==">(function(){window.google.erd={sp:\'hp\',jsr:0,bv:1461,de:true};\nvar f=this||self;var g,h,k=null!==(g=f.mei)&&void 0!==g?g:1,l=null!==(h=f.sdo)&&void 0!==h?h:!0,n=0,p,q=google.erd,t=q.jsr;google.ml=function(a,b,e,m,d){d=void 0===d?2:d;b&&(p=a&&a.message);if(google.dl)return google.dl(a,d,e),null;if(0>t){window.console&&console.error(a,e);if(-2===t)throw a;b=!1}else b=!a||!a.message||"Error loading script"===a.message||n>=k&&!m?!1:!0;if(!b)return null;n++;e=e||{};b=encodeURIComponent;var c="/gen_204?atyp=i&ei="+b(google.kEI);google.kEXPI&&(c+="&jexpid="+b(google.kEXPI));c+="&srcpg="+b(q.sp)+"&jsr="+b(q.jsr)+"&bver="+b(q.bv)+("&jsel="+d);c+="&sn="+b(google.sn);for(var r in e)c+="&",c+=b(r),c+="=",c+=b(e[r]);c=c+"&emsg="+b(a.name+": "+a.message);c=c+"&jsst="+b(a.stack||"N/A");12288<=c.length&&(c=c.substr(0,12288));a=c;m||google.log(0,"",a);return a};window.onerror=function(a,b,e,m,d){p!==a&&google.ml(d instanceof Error?d:Error(a),!1,void 0,!1,!d||d instanceof SyntaxError?2:0);p=null;l&&n>=k&&(window.onerror=null)};})();</script></head><body bgcolor="#fff"><script nonce="dgHg/Go9ku6pd8Q253/V7A==">(function(){var src=\'/images/nav_logo229.png\';var iesg=false;document.body.onload = function(){window.n && window.n();if (document.images){new Image().src=src;}\nif (!iesg){document.f&&document.f.q.focus();document.gbqf&&document.gbqf.q.focus();}\n}\n})();</script><div id="mngb"><div id=gbar><nobr><b class=gb1>&#26908;&#32034;</b> <a class=gb1 href="https://www.google.co.jp/imghp?hl=ja&tab=wi">&#30011;&#20687;</a> <a class=gb1 href="https://maps.google.co.jp/maps?hl=ja&tab=wl">&#12510;&#12483;&#12503;</a> <a class=gb1 href="https://play.google.com/?hl=ja&tab=w8">Play</a> <a class=gb1 href="https://www.youtube.com/?gl=JP&tab=w1">YouTube</a> <a class=gb1 href="https://news.google.com/?tab=wn">&#12491;&#12517;&#12540;&#12473;</a> <a class=gb1 href="https://mail.google.com/mail/?tab=wm">Gmail</a> <a class=gb1 href="https://drive.google.com/?tab=wo">&#12489;&#12521;&#12452;&#12502;</a> <a class=gb1 style="text-decoration:none" href="https://www.google.co.jp/intl/ja/about/products?tab=wh"><u>&#12418;&#12387;&#12392;&#35211;&#12427;</u> &raquo;</a></nobr></div><div id=guser width=100%><nobr><span id=gbn class=gbi></span><span id=gbf class=gbf></span><span id=gbe></span><a href="http://www.google.co.jp/history/optout?hl=ja" class=gb4>&#12454;&#12455;&#12502;&#23653;&#27508;</a> | <a  href="/preferences?hl=ja" class=gb4>&#35373;&#23450;</a> | <a target=_top id=gb_70 href="https://accounts.google.com/ServiceLogin?hl=ja&passive=true&continue=https://www.google.com/&ec=GAZAAQ" class=gb4>&#12525;&#12464;&#12452;&#12531;</a></nobr></div><div class=gbh style=left:0></div><div class=gbh style=right:0></div></div><center><br clear="all" id="lgpd"><div id="lga"><img alt="Google" height="92" src="/images/branding/googlelogo/1x/googlelogo_white_background_color_272x92dp.png" style="padding:28px 0 14px" width="272" id="hplogo"><br><br></div><form action="/search" name="f"><table cellpadding="0" cellspacing="0"><tr valign="top"><td width="25%">&nbsp;</td><td align="center" nowrap=""><input name="ie" value="ISO-8859-1" type="hidden"><input value="ja" name="hl" type="hidden"><input name="source" type="hidden" value="hp"><input name="biw" type="hidden"><input name="bih" type="hidden"><div class="ds" style="height:32px;margin:4px 0"><input class="lst" style="margin:0;padding:5px 8px 0 6px;vertical-align:top;color:#000" autocomplete="off" value="" title="Google &#26908;&#32034;" maxlength="2048" name="q" size="57"></div><br style="line-height:0"><span class="ds"><span class="lsbb"><input class="lsb" value="Google &#26908;&#32034;" name="btnG" type="submit"></span></span><span class="ds"><span class="lsbb"><input class="lsb" id="tsuid1" value="I\'m Feeling Lucky" name="btnI" type="submit"><script nonce="dgHg/Go9ku6pd8Q253/V7A==">(function(){var id=\'tsuid1\';document.getElementById(id).onclick = function(){if (this.form.q.value){this.checked = 1;if (this.form.iflsig)this.form.iflsig.disabled = false;}\nelse top.location=\'/doodles/\';};})();</script><input value="ALs-wAMAAAAAYW4aMXMidQkr3Q1OsPZF6bwURpu1pWOP" name="iflsig" type="hidden"></span></span></td><td class="fl sblc" align="left" nowrap="" width="25%"><a href="/advanced_search?hl=ja&amp;authuser=0">&#26908;&#32034;&#12458;&#12503;&#12471;&#12519;&#12531;</a></td></tr></table><input id="gbv" name="gbv" type="hidden" value="1"><script nonce="dgHg/Go9ku6pd8Q253/V7A==">(function(){\nvar a,b="1";if(document&&document.getElementById)if("undefined"!=typeof XMLHttpRequest)b="2";else if("undefined"!=typeof ActiveXObject){var c,d,e=["MSXML2.XMLHTTP.6.0","MSXML2.XMLHTTP.3.0","MSXML2.XMLHTTP","Microsoft.XMLHTTP"];for(c=0;d=e[c++];)try{new ActiveXObject(d),b="2"}catch(h){}}a=b;if("2"==a&&-1==location.search.indexOf("&gbv=2")){var f=google.gbvu,g=document.getElementById("gbv");g&&(g.value=a);f&&window.setTimeout(function(){location.href=f},0)};}).call(this);</script></form><div id="gac_scont"></div><div style="font-size:83%;min-height:3.5em"><br></div><span id="footer"><div style="font-size:10pt"><div style="margin:19px auto;text-align:center" id="WqQANb"><a href="/intl/ja/ads/">&#24195;&#21578;&#25522;&#36617;</a><a href="http://www.google.co.jp/intl/ja/services/">&#12499;&#12472;&#12493;&#12473; &#12477;&#12522;&#12517;&#12540;&#12471;&#12519;&#12531;</a><a href="/intl/ja/about.html">Google &#12395;&#12388;&#12356;&#12390;</a><a href="https://www.google.com/setprefdomain?prefdom=JP&amp;prev=https://www.google.co.jp/&amp;sig=K_1Z5t4CjoiaF_G0z-LQhynzOQ1VI%3D">Google.co.jp</a></div></div><p style="font-size:8pt;color:#70757a">&copy; 2021 - <a href="/intl/ja/policies/privacy/">&#12503;&#12521;&#12452;&#12496;&#12471;&#12540;</a> - <a href="/intl/ja/policies/terms/">&#35215;&#32004;</a></p></span></center><script nonce="dgHg/Go9ku6pd8Q253/V7A==">(function(){window.google.cdo={height:757,width:1440};(function(){\nvar a=window.innerWidth,b=window.innerHeight;if(!a||!b){var c=window.document,d="CSS1Compat"==c.compatMode?c.documentElement:c.body;a=d.clientWidth;b=d.clientHeight}a&&b&&(a!=google.cdo.width||b!=google.cdo.height)&&google.log("","","/client_204?&atyp=i&biw="+a+"&bih="+b+"&ei="+google.kEI);}).call(this);})();</script> <script nonce="dgHg/Go9ku6pd8Q253/V7A==">(function(){google.xjs={ck:\'\',cs:\'\',excm:[]};})();</script>  <script nonce="dgHg/Go9ku6pd8Q253/V7A==">(function(){var u=\'/xjs/_/js/k\\x3dxjs.hp.en.yMA6JfPP0Iw.O/am\\x3dAPgEWA/d\\x3d1/ed\\x3d1/rs\\x3dACT90oEUAyJIAhQoujvedIOaQ2z8r9B5aA/m\\x3dsb_he,d\';\nvar e=this||self,f=function(a){return a};var g;var l=function(a,b){this.g=b===h?a:""};l.prototype.toString=function(){return this.g+""};var h={};\nfunction m(){var a=u;google.lx=function(){n(a);google.lx=function(){}};google.bx||google.lx()}\nfunction n(a){google.timers&&google.timers.load&&google.tick&&google.tick("load","xjsls");var b=document;var c="SCRIPT";"application/xhtml+xml"===b.contentType&&(c=c.toLowerCase());c=b.createElement(c);if(void 0===g){b=null;var k=e.trustedTypes;if(k&&k.createPolicy){try{b=k.createPolicy("goog#html",{createHTML:f,createScript:f,createScriptURL:f})}catch(p){e.console&&e.console.error(p.message)}g=b}else g=b}a=(b=g)?b.createScriptURL(a):a;a=new l(a,h);c.src=a instanceof l&&a.constructor===l?a.g:"type_error:TrustedResourceUrl";var d;a=(c.ownerDocument&&c.ownerDocument.defaultView||window).document;(d=(b=null===(d=a.querySelector)||void 0===d?void 0:d.call(a,"script[nonce]"))?b.nonce||b.getAttribute("nonce")||"":"")&&c.setAttribute("nonce",d);document.body.appendChild(c);google.psa=!0};setTimeout(function(){m()},0);})();(function(){window.google.xjsu=\'/xjs/_/js/k\\x3dxjs.hp.en.yMA6JfPP0Iw.O/am\\x3dAPgEWA/d\\x3d1/ed\\x3d1/rs\\x3dACT90oEUAyJIAhQoujvedIOaQ2z8r9B5aA/m\\x3dsb_he,d\';})();function _DumpException(e){throw e;}\nfunction _F_installCss(c){}\n(function(){google.jl={attn:false,blt:\'none\',chnk:0,dw:false,dwu:true,emtn:0,end:0,ine:false,lls:\'default\',pdt:0,rep:0,snet:true,strt:0,ubm:false,uwp:true};})();(function(){var pmc=\'{\\x22d\\x22:{},\\x22sb_he\\x22:{\\x22agen\\x22:true,\\x22cgen\\x22:true,\\x22client\\x22:\\x22heirloom-hp\\x22,\\x22dh\\x22:true,\\x22dhqt\\x22:true,\\x22ds\\x22:\\x22\\x22,\\x22ffql\\x22:\\x22ja\\x22,\\x22fl\\x22:true,\\x22host\\x22:\\x22google.com\\x22,\\x22isbh\\x22:28,\\x22jsonp\\x22:true,\\x22msgs\\x22:{\\x22cibl\\x22:\\x22&#26908;&#32034;&#12434;&#12463;&#12522;&#12450;\\x22,\\x22dym\\x22:\\x22&#12418;&#12375;&#12363;&#12375;&#12390;:\\x22,\\x22lcky\\x22:\\x22I\\\\u0026#39;m Feeling Lucky\\x22,\\x22lml\\x22:\\x22&#35443;&#32048;\\x22,\\x22oskt\\x22:\\x22&#20837;&#21147;&#12484;&#12540;&#12523;\\x22,\\x22psrc\\x22:\\x22&#12371;&#12398;&#26908;&#32034;&#12461;&#12540;&#12527;&#12540;&#12489;&#12399;\\\\u003Ca href\\x3d\\\\\\x22/history\\\\\\x22\\\\u003E&#12454;&#12455;&#12502;&#23653;&#27508;\\\\u003C/a\\\\u003E&#12363;&#12425;&#21066;&#38500;&#12373;&#12428;&#12414;&#12375;&#12383;\\x22,\\x22psrl\\x22:\\x22&#21066;&#38500;\\x22,\\x22sbit\\x22:\\x22&#30011;&#20687;&#12391;&#26908;&#32034;\\x22,\\x22srch\\x22:\\x22Google &#26908;&#32034;\\x22},\\x22ovr\\x22:{},\\x22pq\\x22:\\x22\\x22,\\x22refpd\\x22:true,\\x22refspre\\x22:true,\\x22rfs\\x22:[],\\x22sbas\\x22:\\x220 3px 8px 0 rgba(0,0,0,0.2),0 0 0 1px rgba(0,0,0,0.08)\\x22,\\x22sbpl\\x22:16,\\x22sbpr\\x22:16,\\x22scd\\x22:10,\\x22stok\\x22:\\x22wPvWmYjnEXfcKzs6YcrX_2Dx70A\\x22,\\x22uhde\\x22:false}}\';google.pmc=JSON.parse(pmc);})();</script>        </body></html>'

길어서 전체를 다 확인하기는 어렵지만 첫 부분과 마지막 부분에 아래와 같이 html 기본 구조를 갖추고 있는 것을 확인할 수 있습니다. 

 

<!doctype html>

<html itemscope="" itemtype="http://schema.org/WebPage" lang="ja">

</html>

 

 

header 내용 확인하기

HTTP header(헤더)는 클라이언트나 서버가 요청 또는 응답을 할 때 부가적인 정보를 전송할 수 있도록 해줍니다. 위에서 확인한 Response Code도 헤더에 포함된 정보 중 하나입니다. 

 

header의 내용을 출력해보겠습니다. 

print(response.headers)

내용을 확인해보면 아래처럼 '키:값'이 짝을 이루고 있는 딕셔너리형의 데이터라는 것을 확인할 수 있습니다. 

 

{'Date': 'Tue, 19 Oct 2021 00:46:03 GMT', 'Expires': '-1', 'Cache-Control': 'private, max-age=0', 'Content-Type': 'text/html; charset=ISO-8859-1', 'P3P': 'CP="This is not a P3P policy! See g.co/p3phelp for more info."', 'Content-Encoding': 'gzip', 'Server': 'gws', 'X-XSS-Protection': '0', 'X-Frame-Options': 'SAMEORIGIN', 'Set-Cookie': '1P_JAR=2021-10-19-00; expires=Thu, 18-Nov-2021 00:46:03 GMT; path=/; domain=.google.com; Secure, NID=511=DAlhKYdSNOmffCi8iLZc8KDfVBhFnSgsYKvLRjptU3bcbaLBmHXWSJEPG8ydSn7MMdMgoYL7M7hHhqcjkv59PxZ8SlZKHRXficQR7V9SUYh1HhteiHEVOrRyYaDTckqf9-PJXgG_ZmfQy2eX8izarDNohTXYKRVX1zlF3RnqjKY; expires=Wed, 20-Apr-2022 00:46:03 GMT; path=/; domain=.google.com; HttpOnly', 'Alt-Svc': 'h3=":443"; ma=2592000,h3-29=":443"; ma=2592000,h3-T051=":443"; ma=2592000,h3-Q050=":443"; ma=2592000,h3-Q046=":443"; ma=2592000,h3-Q043=":443"; ma=2592000,quic=":443"; ma=2592000; v="46,43"', 'Transfer-Encoding': 'chunked'}

반복문을 사용해서 조금 더 보기 쉽게 출력을 해보겠습니다. 

 

for key, value in response.headers.items():
    print(key, '   ', value)

헤더에는 아래와 같은 정보들이 들어있습니다. 

 

Date     Tue, 19 Oct 2021 00:48:24 GMT
Expires     -1
Cache-Control     private, max-age=0
Content-Type     text/html; charset=ISO-8859-1
P3P     CP="This is not a P3P policy! See g.co/p3phelp for more info."
Content-Encoding     gzip
Server     gws
X-XSS-Protection     0
X-Frame-Options     SAMEORIGIN
Set-Cookie     1P_JAR=2021-10-19-00; expires=Thu, 18-Nov-2021 00:48:24 GMT; path=/; domain=.google.com; Secure, NID=511=d9zVDNhPaPpPFwvKXSIr1sLW7c9d9390X1ES75fC6g4kF_EqCbent_AcHfCZ0KVims7GoWrcop9Tb0VAOkmRpbKfeogoXEiu1axASoDyMeUFEZAKpjYbSYPL_O6vl2HwAYtZczCRMh-FzWOZAuoJ1wL2T3lJQAK7bHAoEEswJcA; expires=Wed, 20-Apr-2022 00:48:24 GMT; path=/; domain=.google.com; HttpOnly
Alt-Svc     h3=":443"; ma=2592000,h3-29=":443"; ma=2592000,h3-T051=":443"; ma=2592000,h3-Q050=":443"; ma=2592000,h3-Q046=":443"; ma=2592000,h3-Q043=":443"; ma=2592000,quic=":443"; ma=2592000; v="46,43"
Transfer-Encoding     chunked

서버가 우리에게 응답을 보낸 날짜(Date)나 쿠키 등에서 사용할 수 있는 기한(Expires)정보 등 다양한 정보들이 포함되어 있습니다. 

 

전체 코드
import requests

response = requests.get('https://www.google.com')

print(response)

print(response.content)

print(response.headers)

for key, value in response.headers.items():
    print(key, '   ', value)
반응형
  • 네이버 블러그 공유하기
  • 네이버 밴드에 공유하기
  • 페이스북 공유하기
  • 카카오스토리 공유하기