주의사항!
본 문서는 Python 3성애자가 다녀간 문서입니다.
본 문서는 Ubuntu를 너무 사랑한 나머지 Ubuntu 18.04.1 LTS기준으로 작성되었습니다.
본 문서에서 크롤링한 한성대학교 공지는 2018.10.03까지 입니다.(관련 data.txt는 bitbucket에 있습니다.
본 문서에서 필요한 모듈들은 다음과 같습니다.
- re
- BeautifulSoup
- requets
- konlpy
- collections
- pytagcloud
- time
time모듈은 크롤링할 때 필수…
간단한 설명!
listparshing이라는 함수가 한성대학교 한성공지 전체를 가져오는 역할 및 타 함수를 실행 후 결과를 data.txt에 기록합니다.
contentparshing이라는 함수는 한성대학교의 세부적인 공지를 가져온 문자열을 가공 후 return 합니다.
urlmaker이라는 함수는 한성대학교 한성공지의 세부적인 링크에 amp;단어들이 하나라도 있을 시 제대로 접속할 수 없는 상황이 나오기 때문에, amp;단어들을 제거해주는 역할을 합니다.
imagemake이라는 함수는 KoNLPy와 Collections의 Counter모듈을 이용해서 형태소별로 나누고, 그것을 Counter를 통해 얼마나 자주 나왔는가 Counting합니다.
결과물
Counter한 결과!
WordCloud!
Python3
import re
from bs4 import BeautifulSoup
import requests
from konlpy.tag import Twitter
from collections import Counter
import time
import pytagcloud
j = 1
def listparshing(url):
r = requests.get(url)
soup = BeautifulSoup(r.text, "html.parser")
hansunglist = soup.find("table", class_="bbs-list")
sublist = hansunglist.find_all("td", class_="subject")
i = 0
try:
while True:
f = open("data.txt", "a")
mainurl = sublist[i].find("a")
mainurl = str(mainurl)
front = mainurl.find("http")
back = mainurl.find(">")
mainurl = mainurl[front:back-1]
new_url = urlmaker(mainurl)
print(new_url)
content = contentparshing(new_url)
i+=1
f.write(content)
f.close()
time.sleep(1)
except IndexError as e:
print(str(j) + " 번째 페이지 끝")
if(i == 0):
return 0
else:
pass
def contentparshing(url):
r = requests.get(url)
soup = BeautifulSoup(r.text, "html.parser")
hansunglist = soup.find("table", class_="bbs-view")
subject = hansunglist.find("th")
subject = str(subject)
cleaner = re.compile('<.*?>')
cleantext = re.sub(cleaner, '', subject)
print(cleantext)
return cleantext
def urlmaker(string):
while True:
if(string.find("amp;") == -1):
return string
else:
a = string.find("amp;")
string1 = string[:a]
string2 = string[a + 4:]
string = string1 + string2
def imagemake():
f = open("data.txt", "r")
text = f.read()
nlp = Twitter()
nouns = nlp.nouns(text)
words = Counter(nouns)
tags = words.most_common(80) #80개만!!! print(tags)를 하면, 아름다운 Counting이...
taglist = pytagcloud.make_tags(tags, maxsize=100)
pytagcloud.create_tag_image(taglist, 'wordcloud.jpg', size=(640 , 480), fontname='Korean', rectangular=True)
f.close()
while True:
url = "http://hansung.ac.kr/web/www/cmty_01_01?p_p_id=EXT_BBS&p_p_lifecycle=0&p_p_state=normal&p_p_mode=view&p_p_col_id=column-1&p_p_col_pos=1&p_p_col_count=3&_EXT_BBS_struts_action=%2Fext%2Fbbs%2Fview&_EXT_BBS_sCategory=&_EXT_BBS_sKeyType=&_EXT_BBS_sKeyword=&_EXT_BBS_curPage=" + str(j)
check = listparshing(url)
if(check == 0):
break
else:
j+=1
imagemake()