본문 바로가기

Programming Languge/Python

HTML Parse

1. 개 요

지정된 웹 페이지 내용에서 사용자가 원하는 부분을 Parsing하여 CSV 파일, JSON 파일, SQLite(DB) 파일로 저장한다. 또한 BeatifulSoup을 활용하여 웹 크롤링으로 사용자가 원하는 데이터를 추출하도록 코딩한다.

2. 내 용

① HTMLParse - CSV

HTML Parse로 데이터 추출 후 CSV 파일로 저장한다.

URL] http://www.hanbit.co.kr/store/books/full_book_list.html

import re
import urllib.request
from html import unescape
import csv

url = "http://www.hanbit.co.kr/store/books/full_book_list.html"
req = urllib.request.urlopen(url)

header = req.info()
encoding = req.info().get_content_charset(failobj="utf-8")
html = req.read().decode(encoding)

with open("hanbit book list.csv", 'w', newline='') as f:
        writer = csv.writer(f)
        writer.writerow(["Title", "URL"])

        for partial_html in re.findall(r'<td class="left"><a.*?</td>', html, re.DOTALL):
            url = re.search(r'<a href="(.*?)">', partial_html).group(1)
            book_url = "http://www.hanbit.co.kr" + url
            #print(book_url)

            title = re.sub(r'<.*?>','', partial_html)
            title = unescape(title)
            #print(title)

            writer.writerow([title, book_url])

 

② HTMLParse - JSON

HTML Parse로 데이터 추출 후 JSON 파일로 저장한다.

URL] http://www.hanbit.co.kr/store/books/full_book_list.html

import json, re
from urllib.request import urlopen
from html import unescape


req = urlopen("http://www.hanbit.co.kr/store/books/full_book_list.html")
encoding = req.info().get_content_charset(failobj="utf-8")
html = req.read().decode(encoding)


with open("test.json", "w", encoding="utf-8") as f:
    data = []


    for partial_html in re.findall(r'<td class="left"><a.*?</td>', html, re.DOTALL):
        url = re.search(r'<a href="(.*?)">', partial_html).group(1)
        url = 'http://www.hanbit.co.kr' + url
        title = re.sub(r'<.*?>', '', partial_html)
        title = unescape(title)


        data.append({"BookName": title, "Link": url})
        print(json.dumps(data, ensure_ascii=False, indent=2))


    json.dump(data, f, ensure_ascii=False, indent=2)

 

③ HTMLParse - SQLite

HTML Parse로 데이터 추출 후 SQLite 파일로 저장한다.

URL] http://www.hanbit.co.kr/store/books/full_book_list.html

import re, sqlite3
from urllib.request import urlopen
from html import unescape

def main():
    html = fetch("http://www.hanbit.co.kr/store/books/full_book_list.html")
    books = scrape(html)
    save("books.db",books)


def fetch(url):
    req = urlopen(url)
    encoding = req.info().get_content_charset(failobj="utf-8")
    html = req.read().decode(encoding)
    return html


def scrape(html):
    books = []
    for partial_html in re.findall(r'<td class="left"><a.*?</td>', html, re.DOTALL):
        url = re.search(r'<a href="(.*?)">', partial_html).group(1)
        url = "http://www.hanbit.co.kr" + url
        title = re.sub(r'<.*?>', '', partial_html)
        title = unescape(title)
        books.append({"url":url, "title":title})
    return books


def save(db_path, books):
    conn = sqlite3.connect(db_path)
    c = conn.cursor()
    c.execute("DROP TABLE IF EXISTS books")
    c.execute(""" CREATE TABLE books(title text,url text)""")
    c.executemany("INSERT INTO books VALUES (:title, :url)", books)
    conn.commit()
    conn.close()

if __name__ == "__main__":
    main()

 

④ BeautifulSoup WEB Crawling

BeautifuSoup을 활용하여 HTML Parse로 데이터 추출 후 엑셀 파일로 저장한다.

--> 네이버 헤드라인 뉴스 정보 추출

URL] https://news.naver.com/main/main.nhn?mode=LSD&mid=shm&sid1=105

from bs4 import BeautifulSoup
from urllib.request import urlopen
import re
import openpyxl


url = "https://news.naver.com/main/main.nhn?mode=LSD&mid=shm&sid1=105"
html = urlopen(url)

wb = openpyxl.Workbook()
sheet = wb.active

bs = BeautifulSoup(html.read(), 'html.parser')
#print(bs)
link = bs.findAll('div', {'class':'cluster_text'})

for li in link:
    headline = li.find('a', href=re.compile('^(https).*$'))
    #print(headline)
    if 'href' in headline.attrs:
        headline_href = headline.attrs['href']

    title = re.sub(r'<.*?>', '', str(headline))
    #print(title)

    #print(headline_href)
    #print(title + "\n")

    sheet.append([title, headline_href])

wb.save('D:/Projects/Project/news headline information.xlsx')

print("Success!!")

 

3. 결 과

01HTMLParse_CSV.py 실행 결과

csv 파일이 저장될 경로를 따로 지정해주지 않았기 때문에 파이썬 파일이 저장되어 있는 경로에 csv 파일이 저장된다. 저장된 hanbit book list.csv 파일을 확인하면 다음과 같다.

01HTMLParse_JSON.py 실행 결과

json 파일이 저장될 경로를 따로 지정해주지 않았기 때문에 파이썬 파일이 저장되어 있는 경로에 json 파일이 저장된다. 저장된 test.json 파일을 확인하면 다음과 같다.

 

01HTMLParse_SQLite.py 실행 결과

db 파일이 저장될 경로를 따로 지정해주지 않았기 때문에 파이썬 파일이 저장되어 있는 경로에 db 파일이 저장된다. 저장된 books.db 파일을 SQLiteDatabaseBrowserPortable 프로그램으로 확인하면 다음과 같다.

BeautifulSoup을 활용한 WEB Crawling 실행 결과

실행한 결과 아래의 그림과 같이 저장 성공했다는 “Success!!” 라는 결과를 볼 수 있다.

또한 지정해준 파일 저장 경로로 가보면 다음과 같이 “news headline informaiont.xlsx”의 엑셀 파일이 저장된 것을 확인할 수 있다.

이 파일을 실행하면 엑셀파일로 저장되어 있는 것을 확인할 수 있다.

 

4. 참고 사이트

https://kimdoky.github.io/python/2017/06/12/python-urllib.html

https://hwangyoungjae.tistory.com/80

https://ekwkqk12.tistory.com/

https://python.bakyeono.net/chapter-11-5.html

https://soooprmx.com/archives/6788

'Programming Languge > Python' 카테고리의 다른 글

영화 예매 프로그램  (0) 2019.09.25