본문 바로가기

DS/fast campus daily report

6.17 (머신러닝과 데이터분석 A-Z) http method 이해하기, html 엘레멘트 이해하기, requests 모듈 사용하기

http method 이해하기(6:27)

  • http (hyper text transfer protocol) 통신규약
  • GET, POST 방식 등 
  • 크롤링 관점에서 GET방식 주로 사용

html 엘레멘트 이해하기(6:15)

  • html (hyper text markup language) : tag를 사용하는 언어
  • html tag : 브라우저에 어떻게 렌더링이 되는지 알려주는 역할
  • 크롤링시 필요한 정보는 tag 사이의 값 (일반적으로)
<p><h3 class="" data-translation>값</h3></p>

 

requests 모듈 사용하기(13:59)

  • 사용이 간단함

GET

import requests

url = 'https://news.v.daum.net/v/20200617071844353'
resp = requests.get(url)

resp.text

''' result
'<!doctype html>\n<html lang="ko">\n <head data-cloud-area="head"> \n  
<style>\n
@import url(\'//t1.daumcdn.net/harmony_static/cloud/page/2603111efaebe3b9879c7b3c1417b82fff251095.css\');\n            @import url(\'//t1.daumcdn.net/harmony_static/cloud/2020/06/11/components.b754a270b74e0b139e21.css\')\n        </style> \n  <meta name="robots" content="noindex"> \n  <meta property="mccp:docId" content="jasSiOFmD4"> \n  <meta property="og:site_name" content="다음 뉴스" data-cloud="pc_html_head_meta"> \n  <meta property="og:title" content="북한군 &quot;금강산·개성공단·GP 군부대 전개&quot;..9.19 군사합의 사실상 파기(종합)" data-cloud="pc_html_head_meta"> \n  <meta property="og:regDate" content="20200617071844" data-cloud="pc_html_head_meta"> \n  
<meta

'''

POST

import requests

url = 'https://news.v.daum.net/v/20200617071844353'
data = {a:1, b:2}
resp = requests.post(data)

HEADER

headers = {}  // dictation data type
resp = requests.get(url, headers=headers)
resp.text

HTTP response

  1. response 객체 이해
  2. status_code 확인
  3. text 속성 확인
resp = requests.get(url)
if resp.status_code == 200:
   resp.headers
else:
   print('error')

학습 후