{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "## HTML을 parsing 하기 - 2" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### BeautifulSoup4 설치." ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Collecting bs4\n", "Requirement already satisfied: beautifulsoup4 in c:\\anaconda3\\lib\\site-packages (from bs4) (4.6.0)\n", "Installing collected packages: bs4\n", "Successfully installed bs4-0.0.1\n" ] } ], "source": [ "!pip install bs4" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "import bs4\n", "import os" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 1. BeautifulSoup 객체로 변환하여 parsing 하도록 한다:" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [], "source": [ "os.chdir(r\"C:\\Users\\Gram\\Desktop\\myPyCode\\02 데이터 수집과 처리 - 실습\\data\")" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [], "source": [ "#컴퓨터에 저장된 html파일로 parshing하기위해 파일 읽어오기\n", "f = open(\"page_02b.html\",\"r\")\n", "my_html = f.read()\n", "f.close()" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n", "\n", " \n", "
\n", " \n", "\n", " Paragraph 1.\n", "
\n", "\n", " Paragraph 2.\n", "
\n", "\n", " Paragraph 3.\n", "
\n", "\n", " Paragraph 4.\n", "
\n", "\n", " Paragraph 5.\n", "
\n", "\n", " Paragraph 6.\n", "
\n", "태그를 찾아서 출력한다.\n", "x=soup.find('p') #첫번째 p태그를 가져옴\n", "print(x.text.strip()) #p태그 텍스트에서 strip" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Paragraph 1.\n", "\n", "Paragraph 2.\n", "\n", "Paragraph 3.\n", "\n", "Paragraph 4.\n", "\n", "Paragraph 5.\n", "\n", "Paragraph 6.\n", "\n", "\n" ] } ], "source": [ "# 모든
태그의 내용을 찾아서 이어 붙여 출력한다.\n", "x=soup.find_all('p') #모든 p태그 가져오기 #x는 리스트성격\n", "n = len(x) #길이로 x의 원소개수 확인\n", "result = '' #빈 문자열 만들어 원소 하나하나 가져옴 # 초기화.\n", "\n", "#x[i]태그객체를 가져와 text속성가져옴\n", "for i in range(n):\n", " result += x[i].text.strip() + '\\n\\n' #문자열 메서드 strip(왼쪽 오른쪽 스페이스 떨궈줌)\n", " #\\n\\n(라인 체인지), +(두개 연결)\n", " \n", "# 출력.\n", "print(result) #p태그 안에 있는 내용만 나옴" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [], "source": [ "type(x) #결과값 ResultSet은 리스트형태로 나옴: 인덱싱해서 원소 가져올 수 있음\n", "type(x[0]) #첫번째 원소 가져옴\n", "\n", "for i in range(n):\n", " result += x[i].text.strip() #인덱싱해 원소가져옴" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 2.2 BeautifulSoup4 라이브러리로 parsing을 한다: div태그" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "A Single line within a div tag. \n", "\n", " Paragraph 4. \n", " Paragraph 5. \n", " Paragraph 6.\n", "\n", "다음은 ordered list이다:\n", "\n", " 아이템 하나. \n", " 아이템 둘. \n", " 아이템 셋. \n", " 아이템 넷. \n", " 아이템 다섯. \n", " 아이템 여섯.\n", "\n", "\n" ] } ], "source": [ "# 모든
Paragraph 1.
\n", "Paragraph 4.
\n", "Paragraph 5.
\n" ] } ], "source": [ "print(x) " ] }, { "cell_type": "code", "execution_count": 16, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ " 아이템 셋. \n", " 아이템 여섯. \n" ] } ], "source": [ "res = soup.find_all('li',class_='myClass3') #li태그면서 myClass3인거만\n", "for x in res: \n", " print(x.text)" ] }, { "cell_type": "code", "execution_count": 17, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "