Skip to content

Commit 302233d

Browse files
committed
refactor module
1 parent 8104df3 commit 302233d

3 files changed

Lines changed: 45 additions & 31 deletions

File tree

‎bigpython/webscrap/__init__.py‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,31 @@
22
import pip
33
import platform, sys, os, stat, zipfile
44
import time
5+
from urllib.parse import urlparse
6+
import re
57

68
import requests
79

810
from . import html, http
911

12+
def get_html(src, encoding='utf-8'):
13+
"""Returns BeautifulSoup from URL or file
14+
15+
src: url or filepath
16+
encoding: (default=utf-8)
17+
"""
18+
# check if url or filepath
19+
scheme = urlparse(src).scheme
20+
if re.compile('(http|https)').match(scheme):
21+
res = requests.get(src)
22+
doc = res.text
23+
else:
24+
doc = open(src, encoding=encoding)
25+
26+
# select parser for BeautifulSoup
27+
# return BeautifulSoup(doc, parser)
28+
return html.Html(doc)
29+
1030
def get_browser(browser='Chrome', driverpath=None):
1131
driver_map = {'Chrome': 'chromedriver'}
1232

‎bigpython/webscrap/html.py‎

Lines changed: 14 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -7,24 +7,20 @@
77
import pandas as pd
88
from bs4 import BeautifulSoup
99

10-
def get_soup(src, encoding='utf-8'):
11-
"""Returns BeautifulSoup from URL or file
10+
class Html(BeautifulSoup):
11+
def __init__(self, html):
12+
parser = 'lxml' if importlib.find_loader('lxml') else 'html.parser'
13+
super().__init__(html, parser)
1214

13-
src: url or filepath
14-
encoding: (default=utf-8)
15-
"""
16-
# check if url or filepath
17-
scheme = urlparse(src).scheme
18-
if re.compile('(http|https)').match(scheme):
19-
res = requests.get(src)
20-
doc = res.text
21-
else:
22-
doc = open(src, encoding=encoding)
15+
def get_text(self, selector='p'):
16+
elements = self.select(selector)
17+
text = ''
18+
for e in elements:
19+
text += e.text
2320

24-
# select parser for BeautifulSoup
25-
parser = 'lxml' if importlib.find_loader('lxml') else 'html.parser'
26-
return BeautifulSoup(doc, parser)
21+
return text
2722

28-
def extract_tables(table_elements):
29-
table_frames = pd.read_html(str(table_elements))
30-
return table_frames
23+
def extract_tables(self, selector):
24+
table_elements = self.select(selector)
25+
table_frames = pd.read_html(str(table_elements))
26+
return table_frames[0] if len(table_frames) == 1 else table_frames

‎tests/test_html.py‎

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,31 @@
11
# coding: utf-8
22
import os
33

4-
from bs4 import BeautifulSoup
5-
4+
from bigpython import webscrap
65
from bigpython.webscrap import http, html
76

87
def test_extract_table():
98
url = 'https://ko.wikipedia.org/wiki/%EC%95%A8%EB%9F%B0_%ED%8A%9C%EB%A7%81'
109
filename = http.download(url, 'turing_wiki.html')
1110
with open(filename) as f:
12-
soup = BeautifulSoup(f, 'html.parser')
11+
html_doc = html.Html(f)
1312
# 파일 삭제
1413
os.unlink(filename)
1514

16-
tables = soup.select('table.wikitable')
17-
18-
table_frames = html.extract_tables(tables)
15+
table_frames = html_doc.extract_tables('table.wikitable')
1916
assert table_frames[0].ix[0,0] == '1912년'
2017

21-
def test_get_soup():
18+
def test_get_html():
2219
# source is url
2320
url = 'https://ko.wikipedia.org/wiki/%EC%95%A8%EB%9F%B0_%ED%8A%9C%EB%A7%81'
24-
soup = html.get_soup(url)
25-
assert soup.title.text == "앨런 튜링 - 위키백과, 우리 모두의 백과사전"
21+
html_doc = webscrap.get_html(url)
22+
assert html_doc.title.text == "앨런 튜링 - 위키백과, 우리 모두의 백과사전"
2623

2724
# source is file
2825
filename = http.download(url, 'turing_wiki.html')
29-
soup = html.get_soup(filename)
30-
# 파일 삭제
31-
os.unlink(filename)
26+
html_doc = webscrap.get_html(filename)
27+
28+
assert html_doc.title.text == "앨런 튜링 - 위키백과, 우리 모두의 백과사전"
3229

33-
assert soup.title.text == "앨런 튜링 - 위키백과, 우리 모두의 백과사전"
30+
# cleanup
31+
os.unlink(filename)

0 commit comments

Comments
 (0)