728x90
반응형

간단하게 url을 통해 HTML 페이지를 요청하고 HTML 페이지를 파싱해서 title 태그를 가져오는 실습을 진행해보았다.
실습한 과정을 간단하게 정리해본다.

 

1. 파이썬 설치하기

파이썬을 설치하는 방법은 여러가지가 있는데  Homebrew 를 사용해서 설치해보았다.
Homebrew intall for MacOS : brew.sh/#install

/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install.sh)"

homebrew 설치 후 파이썬 설치

brew install python

python 설치 후 버전 확인

python --version

2. beautifulsoup4 , requests 라이브러리 설치하기

# requests 라이브러리 설치
pip intall requests

# beautifulsoup4 라이브러리 설치
pip intall beautifulsoup4

3. 라이브러리 import 하기

import requests
from bs4 import BeautifulSoup

4. requests 라이브러리 사용해서 HTML 페이지 가져오기

requests guide : requests.readthedocs.io/en/master/

# requests 라이브러리를 사용해서 HTML 페이지를 요청한다.
# res 객체에 HTML 데이터가 저장되고, res.content 로 데이터 추출
res = requests.get('https://www.naver.com')

5. beautifulsoup4 라이브러리 사용해서 HTML 파싱하기

# HTML 소스를 가져온다.
soup = BeautifulSoup(res.content, 'html.parser')

6. HTML 파싱 후 title 태그 가져오기

# HTML 소스에서 title 태그를 가져온다.
# find 메소드를 통해서 태그를 검색할수 있다.
title = soup.find('title')

7, 타이틀 출력하기

# title 태그의 text 출력
print(title.get_text())

 

<python code>

import requests
from bs4 import BeautifulSoup

# requests 라이브러리를 사용해서 HTML 페이지를 요청한다.
# res 객체에 HTML 데이터가 저장되고, res.content 로 데이터 추출
res = requests.get('https://www.naver.com')

# HTML 소스를 가져온다.
soup = BeautifulSoup(res.content, 'html.parser')

# HTML 소스에서 title 태그를 가져온다.
# find 메소드를 통해서 태그를 검색할수 있다.
title = soup.find('title')

# title 태그의 text 출력
print(title.get_text())
728x90
반응형
복사했습니다!