Skip to content

Commit 263425b

Browse files
committed
io.py --> http.py
1 parent f9d9c7c commit 263425b

6 files changed

Lines changed: 47 additions & 67 deletions

File tree

‎bigpython/webscrap/__init__.py‎

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
# coding: utf-8
2-
from . import io, html
3-
42
import pip
53
import platform, sys, os, stat, zipfile
6-
import requests
74
import time
85

6+
import requests
7+
8+
from . import html, http
9+
910
def get_browser(browser='Chrome', driverpath=None):
1011
driver_map = {'Chrome': 'chromedriver'}
1112

@@ -72,17 +73,5 @@ def test_webdriver(driverfile):
7273
time.sleep(10)
7374
chrome.quit()
7475

75-
def http_download(url, 파일경로):
76-
응답 = requests.get(url)
77-
if os.path.exists(파일경로):
78-
print('File already exists')
79-
return 파일경로
80-
81-
with open(파일경로, 'wb') as 파일:
82-
for 조각 in 응답.iter_content(100000):
83-
파일.write(조각)
84-
85-
return 파일경로
86-
8776
if __name__ == '__main__':
8877
setup_webdriver()

‎bigpython/webscrap/html.py‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
from urllib.parse import urlparse
33
import re
44
import importlib
5-
from . import io
65

6+
import requests
77
import pandas as pd
88
from bs4 import BeautifulSoup
99

@@ -16,7 +16,7 @@ def get_soup(src, encoding='utf-8'):
1616
# check if url or filepath
1717
scheme = urlparse(src).scheme
1818
if re.compile('(http|https)').match(scheme):
19-
res = io.http_download(src)
19+
res = requests.get(src)
2020
doc = res.text
2121
else:
2222
doc = open(src, encoding=encoding)

‎bigpython/webscrap/http.py‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# coding: utf-8
2+
import sys
3+
import os
4+
from urllib.parse import urlparse, unquote, unquote_plus
5+
6+
import requests
7+
8+
def download(url, filepath, chunksize=100000):
9+
res = requests.get(url)
10+
res.raise_for_status()
11+
if os.path.exists(filepath):
12+
print('File already exists')
13+
return filepath
14+
15+
with open(filepath, 'wb') as f:
16+
for chunk in res.iter_content(chunksize):
17+
f.write(chunk)
18+
19+
return filepath
20+
21+
def unquote_url(url):
22+
if '+' in url:
23+
return unquote_plus(url)
24+
return unquote(url)

‎bigpython/webscrap/io.py‎

Lines changed: 0 additions & 35 deletions
This file was deleted.
Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
# coding: utf-8
2-
from . import io
3-
from . import html
4-
from bs4 import BeautifulSoup
52
import os
63

4+
from bs4 import BeautifulSoup
5+
6+
from bigpython.webscrap import http, html
7+
78
def test_extract_table():
89
url = 'https://ko.wikipedia.org/wiki/%EC%95%A8%EB%9F%B0_%ED%8A%9C%EB%A7%81'
9-
filename = io.snapshot_webpage(url, 'turing_wiki.html')
10+
filename = http.download(url, 'turing_wiki.html')
1011
with open(filename) as f:
1112
soup = BeautifulSoup(f, 'html.parser')
1213
# 파일 삭제
@@ -24,7 +25,7 @@ def test_get_soup():
2425
assert soup.title.text == "앨런 튜링 - 위키백과, 우리 모두의 백과사전"
2526

2627
# source is file
27-
filename = io.snapshot_webpage(url, 'turing_wiki.html')
28+
filename = http.download(url, 'turing_wiki.html')
2829
soup = html.get_soup(filename)
2930
# 파일 삭제
3031
os.unlink(filename)
Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,41 @@
11
# coding: utf-8
2-
from . import io
32
import pytest
43
import requests
54
import os
65
from urllib.parse import quote_plus
76

7+
from bigpython.webscrap import http
8+
89
def test_http_download():
9-
res = io.http_download('http://google.com')
10+
res = http.download('http://google.com')
1011
assert res.status_code == 200
1112

1213
with pytest.raises(requests.ConnectionError):
13-
io.http_download('http://no-such-domain.com')
14+
http.download('http://no-such-domain.com')
1415

1516
with pytest.raises(requests.HTTPError):
16-
io.http_download('http://google.com/nosuchpath')
17+
http.download('http://google.com/nosuchpath')
1718

1819
def test_unquote_url():
1920
url = 'https://ko.wikipedia.org/wiki/%EC%95%A8%EB%9F%B0_%ED%8A%9C%EB%A7%81'
20-
url_unquoted = io.unquote_url(url)
21+
url_unquoted = http.unquote_url(url)
2122
basename = os.path.basename(url_unquoted)
2223
assert basename == '앨런_튜링'
2324

2425
url_quote_plus = quote_plus(url_unquoted)
25-
url_unquoted = io.unquote_url(url_quote_plus)
26+
url_unquoted = http.unquote_url(url_quote_plus)
2627
basename = os.path.basename(url_unquoted)
2728
assert basename == '앨런_튜링'
2829

29-
def test_snapshot_webpage():
30+
def test_http_download():
3031
url = 'https://ko.wikipedia.org/wiki/%EC%95%A8%EB%9F%B0_%ED%8A%9C%EB%A7%81'
31-
filename = io.snapshot_webpage(url, '앨런튜링_위키.html')
32+
filename = http.download(url, '앨런튜링_위키.html')
3233
assert os.path.exists(filename)
3334
# cleanup
3435
os.unlink(filename)
3536

3637
with pytest.raises(requests.ConnectionError):
37-
io.snapshot_webpage('http://no-such-domain.com', 'no.html')
38+
http.download('http://no-such-domain.com', 'no.html')
3839

3940
with pytest.raises(requests.HTTPError):
40-
io.http_download('http://google.com/nosuchpath')
41+
http.download('http://google.com/404', 'no.html')

0 commit comments

Comments
 (0)