.

파이썬 혼자놀기 패키지 (무료 강의)

3일 차 - 수강 일자 210930

 

from wordcloud import WordCloud

text = ''
with open("kakaotalk.txt", "r", encoding="utf-8") as f:
    lines = f.readlines()
    for line in lines:
        text += line

wc = WordCloud(font_path='/System/Library/Fonts/AppleSDGothicNeo.ttc', background_color="white", width=600, height=400)
wc.generate(text)
wc.to_file("result.png")

 

코드 설명

from wordcloud import WordCloud

워드클라우드 라이브러리를 사용한다.

 

text = ''
with open("kakaotalk.txt", "r", encoding="utf-8") as f:

    lines = f.readlines()
    for line in lines:
    text += line

text를 공란으로 만들어주고, kakaotalk.txt라는 파일을 오픈한다. (txt파일이라면 어떤 파일도 무방하다. 카카오톡 톡 내보내기를 통해 사용하는 것을 강의에서 권하고 있으나, 그럴 경우 csv파일로 export되어 적용이 안된다.)

kakaotalk.txt파일의 모든 줄들을 읽어서 한줄한줄을 text에 채워준다.

 

wc = WordCloud(font_path='/System/Library/Fonts/AppleSDGothicNeo.ttc', background_color="white", width=600, height=400)
wc.generate(text)
wc.to_file("result.png")

text에 워드클라우드 함수를 적용한다.

폰트를 설정하고, 백그라운드 컬러와 가로 세로 크기를 결정한다.

저장할 파일 명을 결정한다. result.png

 

🔥 이때 폰트 경로 설정이 중요하다.

아래의 코드를 통해 폰트들의 링크를 가져올 수 있다. 왜인지 모르게 ttf type의 폰트는 적용이 안돼서, ttc파일을 적용시켰다.

 

폰트 경로 가져오기

import matplotlib.font_manager as fm

for font in fm.fontManager.ttflist:
    if 'Gothic' in font.name:
        print(font.name, font.fname)

폰트 경로 출력 결과

Apple SD Gothic Neo /System/Library/Fonts/AppleSDGothicNeo.ttc
Hiragino Maru Gothic Pro /System/Library/Fonts/ヒラギノ丸ゴ ProN W4.ttc
Noto Sans Gothic /System/Library/Fonts/Supplemental/NotoSansGothic-Regular.ttf
AppleGothic /System/Library/Fonts/Supplemental/AppleGothic.ttf

위의 링크에서 /로 시작하는 폰트 링크를 복사해 위의 내용을 font_path에 넣으면 된다.

 

if, 혹시 폰트 경로만 출력하고싶다면 아래의 코드를 입력하면 된다.

import matplotlib.font_manager as fm

for font in fm.fontManager.ttflist:
    if 'Gothic' in font.name:
        print(font.fname)

 

출력 결과

.

결과를 보면 무의미한 내용이 엄청 많이 들어있다. ㅋㅋㅋ, hk 이런내용들이다.

여기서 무의미한 데이터들을 필터링해주는 데이터클리닝 과정을 진행한다.

 

데이터 클리닝

text = ''
with open("kakaotalk.txt", "r", encoding="utf-8") as f:
    lines = f.readlines()
    for line in lines[5:]: 
        if '] [' in line: 
        text += line.split('] ')[2].replace('ㅋ','').replace('ㅜ','').replace('이모티콘\n','').replace('삭제된 메시지입니다','')

for line in lines[5:]:

5번째 줄부터 출력 (초반에 불필요한 메시지들 제외)

 

if '] [' in line:

사람들이 말한 경우만 포함하기 = 시스템 명령어 제거하기

파일 구조에 시스템 명령어들은 ] [를 포함하고 있지 않고, 사람이 말한 경우 "[말한사람이름] [시간] 말한 내용 "이렇게 돼서 위의 트릭으로 사람이 말한 경우만 포함 할 수 있다.

 

line.split('] ')[2]

파일 구조가 "ex. [정훈hk] [오후 5:10] 왜 우울한지 고르시오"이런식으로 생겨서 '] '를 기준으로 짜르면 text부분이 2순서에 위치함. 일종의 트릭

 

.replace('ㅋ','').replace('ㅜ','').replace('이모티콘\n','').replace('삭제된 메시지입니','')

위와 같은 이유와 목적으로 여러가지 무의미한 내용들을 삭제한다.

 

 

다시 워드클라우드

이렇게 클리닝된 내용을 기반으로 워드클라우드를 다시 해보자

from wordcloud import WordCloud

text = ''
with open("kakaotalk.txt", "r", encoding="utf-8") as f:
    lines = f.readlines()
    for line in lines[5:]:
        if '] [' in line:
        text += line.split('] ')[2].replace('ㅋ','').replace('ㅜ','').replace('이모티콘\n','').replace('삭제된 메시지입니다','').replace('저두','').replace('저도','').replace('근데','').replace('나','').replace('아','')

wc = WordCloud(font_path='/System/Library/Fonts/AppleSDGothicNeo.ttc', background_color="white", width=600, height=400)
wc.generate(text)
wc.to_file("result4.png")

 

출력 결과

.

전보다는 훨씬 유의미한 내용들만 남은것을 볼 수 있다.

 

원하는 모양으로 워드클라우드

원하는 형태의 이미지를 import 해서 워드클라우드를 만들자

from wordcloud import WordCloud
from PIL import Image
import numpy as np

text = ''
with open("kakaotalk.txt", "r", encoding="utf-8") as f:
    lines = f.readlines()
    for line in lines[5:]:
        if '] [' in line:
        text += line.split('] ')[2].replace('ㅋ','').replace('ㅜ','').replace('이모티콘\n','').replace('삭제된 메시지입니','').replace('저두','').replace('저도','').replace('근데','').replace('나','').replace('아','')

mask = np.array(Image.open('cloud.png'))
wc = WordCloud(font_path='/System/Library/Fonts/AppleSDGothicNeo.ttc', background_color="white", mask=mask)
wc.generate(text)
wc.to_file("result_masked.png")

 

적용시킨 이미지 (cloud.png)

.

 

출력 결과

.

wordcloud 함수는 이미지상의 '검은색영역'에 워드클라우드를 만드는 것을 볼 수 있다.

 

다른 이미지

다른 이미지 출처 

 

위처럼 형태가 다른 이미지를 넣어서 적용할 경우

.

역시 검정 영역에 채워지는 결과를 볼 수 있다.

검정과 흰색의 대비가 애매한 이미지를 사용하면 위의 사각형과 동일한 결과를 낸다.

 

.


 

강의 회고록으로 이어집니다. 👇

(회고록 링크 추가예정)


스파르타 코딩클럽 5만 원 할인 쿠폰 링크 💵

아래 링크를 통해 가입하시면, 즉시 5만원 할인 쿠폰이 발급됩니다!

 

스파르타코딩클럽

왕초보 8주 완성! 웹/앱/게임 빠르게 배우고 내것을 만드세요!

spartacodingclub.kr

 

.

파이썬 혼자놀기 패키지 (무료 강의)

2일 차 - 수강 일자 210926-29

 

 

(1) 기사 크롤링

목표 : 네이버에서 '추석' 뉴스 검색결과의 기사 제목, URL, 언론사 명을 크롤링한다.

 

전체코드

from bs4 import BeautifulSoup
from selenium import webdriver

driver = webdriver.Chrome('./chromedriver')

url = "https://search.naver.com/search.naver?where=news&sm=tab_jum&query=추석"

driver.get(url)
req = driver.page_source
soup = BeautifulSoup(req, 'html.parser')

articles = soup.select('#main_pack > section.sc_new.sp_nnews._prs_nws > div > div.group_news > ul > li')

for article in articles:
    title = article.select_one('div.news_wrap.api_ani_send > div >  a').text 
    url = article.select_one('div.news_wrap.api_ani_send > div >  a')['href'] 
    press = article.select_one('a.info.press').text.split(' ')[0].replace('언론사','') 
    print(title, url, press)

driver.quit()

코드 설명

from bs4 import BeautifulSoup
from selenium import webdriver

library 로 beautifulsoup4, selnium을 사용한다.

사용에 앞서 chrome webdriver를 다운로드 해야한다. (다운로드 방법은 아래의 글 참조)

 

파이썬 파이참 웹 크롤링용 크롬 웹드라이버 chromedriver 설치 - 인강이랑 버전 안맞을 때

문제 현재 스파르타 코딩클럽 파이썬 혼자 놀기 강의 같은 경우, chrome 85 버전으로 진행되어서 chrome 버전이 지금 버전이랑 차이가 있습니다. 1일차 강의 기록 - 파이썬 문법 기초, 웹 크롤링, 뷰티

slowslow.tistory.com

 

url = "https://search.naver.com/search.naver?where=news&sm=tab_jum&query=추석"
네이버에서 "추석"을 검색한 뉴스 결과를 url 로 삽입한다.

 

driver.get(url)
req = driver.page_source
soup = BeautifulSoup(req, 'html.parser')

driver에 url을 입력하고, 그안에 소스를 가져와 req에 넣고 beautifulsoup에 적용시킨다.

 

articles = soup.select('#main_pack > section.sc_new.sp_nnews._prs_nws > div > div.group_news > ul > li')

네이버 검색결과는 구조가
sp_news 1 > ....
sp_news 5 > ...
이렇게되어있음.
그래서 앞에서 이미지 크롤링 한 것처럼 {i}반복구문을 사용할 수 없음
다른 방법은, 해당 기사 위에 위에 있는 ul 의 selector를 copy 해서 그 안의 li를 출력해라 라고 구문을 써주면됨 위 처럼

 

🔥 검색 포털, 사이트마다의 url 구조가 모두 달라서, 각 페이지마다의 상황을 고려해가면서 코드를 작성해주어야한다. 위 코드로 구글, 다음 등에서 진행하면 error 가 발생한다.

 

for article in articles:
title = article.select_one('div.news_wrap.api_ani_send > div > a').text 
url = article.select_one('div.news_wrap.api_ani_send > div > a')['href'
press = article.select_one('a.info.press').text.split(' ')[0].replace('언론사',''
print(title, url, press)

 

- title 내용 : 나온 내용중에서 text만 필요해서 .text를 붙였다 .text는 여기 안붙이고 아래에 title.text로 해도 된다.

- url 내용 : url을 추가했음. url은 dictionary형태로 되어있어서(?) key값인 href를 넣어주니 value값인 url을 뱉어냄. copy selector 해서 붙여넣기

- press 내용 : 서, 가장 낮은단위로 a.info.press를 해보고 안되면 다른거 하려고 했는데 이걸로 돼서 진행함. 안되면 여러가지 시도해야됨. 그리고 .text를 붙여서 텍스트만 가져오고, 띄어쓰기를 기준으로 '선정'이라는 내용이 뒤에 붙어있어서 그부분 split으로 제거하고 앞에만 가져온다음에, replace로 '언론사'를 삭제

 

driver.quit()

driver 종료함! end.

 

출력 결과

추석 이후 서울 외곽지역 집값 상승 주도…강북·구로 높은 상승률 http://www.newsis.com/view/?id=NISX20211001_0001600454&cID=10401&pID=10400 뉴시스
반도체의 힘…수출, 추석 휴무에도 65년 만 최대치 https://www.joongang.co.kr/article/25011561 중앙일보
반도체·추석연휴에 현대차 판매 22.3%↓..생산일정 조정 http://news.mt.co.kr/mtview.php?no=2021100115384196008 머니투데이
나주시, 추석 연휴 이후 24명 신규확진...코로나 방역관리 촉각 http://www.sportsseoul.com/news/read/1065993?ref=naver 스포츠서울
추석 후폭풍에 거리두기 유지 ... 결혼식, 돌잔치 등만 인원 추가 허용 https://hankookilbo.com/News/Read/A2021100111130003341?did=NA 한국일보
추석 후 감염 확산세…충북 거리두기 3단계 2주 연장 https://www.news1.kr/articles/?4449640 뉴스1
전국 아파트값 상승폭 주춤 “추석 및 금리 인상 영향” http://www.fnnews.com/news/202109301159398552 파이낸셜뉴스
추석 연휴·대출규제, 아파트 매매·전셋값 상승세 꺾여 http://www.inews24.com/view/1408105 아이뉴스24
서울 일주일째 '요일 최다'…추석 감염확산 현실로(종합) http://www.edaily.co.kr/news/newspath.asp?newsid=02279606629185368 이데일리
'추석후 1주일 만에' 학생 1828명 확진…하루 261명꼴 http://www.newsis.com/view/?id=NISX20210930_0001598924&cID=10201&pID=10200 뉴시스

 

(2) 크롤링 결과 excel에 저장하기

목표 : 네이버에서 '추석' 뉴스 검색결과의 기사 제목, URL, 언론사 명을 크롤링한 결과물을 엑셀에 저장한다.

 

전체 코드

from bs4 import BeautifulSoup
from selenium import webdriver
from openpyxl import Workbook

driver = webdriver.Chrome('./chromedriver')

url = "https://search.naver.com/search.naver?where=news&sm=tab_jum&query=추석"

driver.get(url)
req = driver.page_source
soup = BeautifulSoup(req, 'html.parser')

articles = soup.select('#main_pack > section.sc_new.sp_nnews._prs_nws > div > div.group_news > ul > li') #copy selector

wb = Workbook()
ws1 = wb.active
ws1.title = "articles"
ws1.append(["제목", "링크", "신문사"])

for article in articles:
    title = article.select_one('div.news_wrap.api_ani_send > div >  a').text 
    url = article.select_one('div.news_wrap.api_ani_send > div >  a')['href']
    press = article.select_one('a.info.press').text.split(' ')[0].replace('언론사','')

    ws1.append([title, url, press])

driver.quit()
wb.save(filename='articles.xlsx')

코드 설명

(1)의 내용을 기반으로 한다.

 

from openpyxl import Workbook

openpyxl 이라는 library를 사용한다. (설치방법은 아래의 링크 참조)

 

파이참 패키지 import 하는법 - 셀레니움(selenium), dload, 뷰티풀숩 (beautifulsoup) (m1 맥북 ver)

파이썬 패키지 import 하는 법 (m1 맥북 ver) 1. pycharm 파이참을 실행 후, 상단의 Pycharm 클릭 2. Pycharm 클릭시 나오는 버튼 중 Preferences 클릭 단축키 : command + , 3. 왼쪽 탭에서, 작업중인 Project..

slowslow.tistory.com

 

wb = Workbook()
ws1 = wb.active
ws1.title = "articles"
ws1.append(["제목", "링크", "신문사"])

wb라는 workbook을 만든다.

ws1의 title은 articles이고, 첫행에 제목, 링크, 신문사 열을 추가한다.

 

ws1.append([title, url, press])

ws1에 title, url, press에 값을 넣어준다. 

 

wb.save(filename='articles.xlsx')

file을 저장한다. 파일명은 'articles.xlsx'

 

출력 결과

출력결과

 

(3) 기사의 썸네일까지 가져와서 엑셀에 저장하기

목표 : 네이버에서 '추석' 뉴스 검색결과의 기사 제목, URL, 언론사 명, 썸네일주소를 크롤링한다.

 

전체코드

from bs4 import BeautifulSoup
from selenium import webdriver
from openpyxl import Workbook

driver = webdriver.Chrome('./chromedriver')

url = "https://search.naver.com/search.naver?&where=news&query=추석"

driver.get(url)
req = driver.page_source
soup = BeautifulSoup(req, 'html.parser')

articles = soup.select('#main_pack > section.sc_new.sp_nnews._prs_nws > div > div.group_news > ul > li')

wb = Workbook()
ws1 = wb.active
ws1.title = "articles"
ws1.append(["제목", "링크", "신문사", "썸네일"])

for article in articles:
    title = article.select_one('div.news_wrap.api_ani_send > div >  a').text
    url = article.select_one('div.news_wrap.api_ani_send > div >  a')['href']
    press = article.select_one('a.info.press').text.split(' ')[0].replace('언론사','')
    #thumb = article.select_one('div.news_wrap.api_ani_send > div >  a> img')['src']
    thumb = article.select_one('div.news_wrap.api_ani_send > a >img')['src']

    ws1.append([title, url, press, thumb])

driver.quit()
wb.save(filename='articles.xlsx')

다른 내용은 다맞는데 이상하게 thumb내용이 오류가 난다. (해결법 찾아서 수정 추가예정)

#thumb = articles.select_one ~ 에서 div 를 제거하니 정상 실행된다.

 

출력 결과

.


스파르타 코딩클럽 5만 원 할인 쿠폰 링크 💵

아래 링크를 통해 가입하시면, 즉시 5만원 할인 쿠폰이 발급됩니다!

 

스파르타코딩클럽

왕초보 8주 완성! 웹/앱/게임 빠르게 배우고 내것을 만드세요!

spartacodingclub.kr

 

.

문제

현재 스파르타 코딩클럽 파이썬 혼자 놀기 강의 같은 경우, chrome 85 버전으로 진행되어서 chrome 버전이 지금 버전이랑 차이가 있습니다.

 

1일차 강의 기록 - 파이썬 문법 기초, 웹 크롤링, 뷰티풀 숩, 셀레니움, 웹 스크래핑, 이미지 자동

파이썬 혼자놀기 패키지 (무료 강의) 1일 차 - 수강 일자 210925 스파르타 코딩 클럽에서 단기적으로 진행중인 [힙한 취미 코딩 이벤트]의 무료강의중 하나인 파이썬 웹크롤링 강의를 수강중이다.

slowslow.tistory.com

 

크롬 버전

저의 경우 93.0.4577.82 버전인데 강의에서 안내해주는 크롬 드라이버 링크

https://chromedriver.storage.googleapis.com/index.html?path=85.0.4183.87/

에는 정확하게 93.0.4577.82버전에 맞는 크롬 드라이버가 안내되어있지 않습니다.

 

링크 화면

93 버전은 있지만, 4577 이후 버전이 다른 것을 확인할 수 있는데요 

 

 

해결

크롬드라이버 원본 페이지 링크를 활용하면 됩니다.

https://chromedriver.chromium.org/downloads

 

chrome driver

크롬 드라이버 페이지를 참조해보면, 

If you are using Chrome version 93, please download Chromedriver 93.0.4577.63

이라는 안내가 있습니다. 

 

따라서, 저처럼 버전이 조금 안 맞는 경우에도 93으로 시작하는 버전이면 93.0.4577.63 버전을 다운로드하면 됩니다.

 

스파르타코딩클럽에서 준 링크에는 없지만, 본 페이지에서는 크롬 상위 버전 단위로 드라이버 선택 가이드가 있습니다.

위 내용들 참조해서 다운로드 하시면 되겠습니다.


스파르타 코딩클럽 5만 원 할인 쿠폰 링크 💵

아래 링크를 통해 가입하시면, 즉시 5만 원 할인 쿠폰이 발급됩니다!

 

스파르타코딩클럽

왕초보 8주 완성! 웹/앱/게임 빠르게 배우고 내것을 만드세요!

spartacodingclub.kr

 

.

파이썬 패키지 import 하는 법 (m1 맥북 ver)

 

1. pycharm 파이참을 실행 후, 상단의 Pycharm 클릭

pycharm 클릭

 

2. Pycharm 클릭시 나오는 버튼 중 Preferences 클릭 

단축키 : command + , 

Preferences 클릭

 

3. 왼쪽 탭에서, 작업중인 Project* 하단에 Python Interpreter 클릭 > 우측에 + 플러스 버튼** 클릭

 

* 저는 sparta_python 이라는 폴더에 작업 중이라 project 명이 sparta_python으로 나와있습니다.

** pycharm 버전에 따라 + 버튼 위치가 다를 수 있습니다.

 

interpreter > +

4. import 해야하는 package 검색 > install Package 클릭 > 기다림 > 완료

sparta python 웹 크롤링 강의를 위해서는 아래의 3가지 package를 다운로드해야 합니다.

- selenium

- bs4
- dload 

install

 


스파르타 코딩클럽 5만 원 할인 쿠폰 링크 💵

아래 링크를 통해 가입하시면, 즉시 5만 원 할인 쿠폰이 발급됩니다!

 

스파르타코딩클럽

왕초보 8주 완성! 웹/앱/게임 빠르게 배우고 내것을 만드세요!

spartacodingclub.kr

 

+ Recent posts