
데이터 분석 실무 with 파이썬 05장 인스타그램 웹크롤링 복습
import unicodedata
content = soup.select('div._aat6 > ul')[0].text
content = unicodedata.normalize('NFC', content)
content
>> unicodedata.normalize(form, unistr, /)
: Return the normal form 'form' for the Unicode string unistr.
한글 자음/모음 분리되는 현상 방지 하기 위해 unicodedata를 이용해 자음/모음을 합쳐서 한글을 처리하는 (NFC 방식)
코드를 추가
import re
re.findall(r'#[^\s#,\\]+', content)
- r : r ' ' 사이에 있는 것들을 문자로 인식
- [] : or ( 또는 ) 의미, s : space
- ^ : not
- # 다음에 무슨 스페이스나 역슬래시 나온다던가 , 나온다던가 제외하고 뽑아줘
date = soup.select('time._a9ze._a9zf')[0]['datetime'][:10]
date
- 작성일자 가져오기
# 작성일자 가져오기
date = soup.select('time._a9ze._a9zf')[0]['datetime'][:10]
date
>> soup.selct('time._a9ze._a9zf') : time 태그의 class명이 _a0ze, _a9zf인 것을 모두 선택
[0] 첫 번째 태그
['datetime'] : 해당 태그의 datetime 속성값
[:10] : 앞에서부터 10자리 글자
- 좋아요 수 가져오기
like = soup.select('div._aacl._aaco._aacu._aacx._aad6._aade')[0].text[14:18]
like
>> soup.select('div._aacl.~~') : div 태그의 class명이 _aacl._...._aade 인 것들 선택
[0] : 첫 번째 태그를 선택
text : 해당 태그의 텍스트
[14:18] : 14번째 부터 17까지'
like = soup.select('div._aacl._aaco._aacu._aacx._aad6._aade')[0].text.split(' ')[1]
like
>> 해당 텍스트를 ' '(빈 칸)을 기준으로 2번째 값?
- 댓글 가져오기
fb = soup.select('div._a9zs > span._aacl._aaco._aacu._aacx._aad7._aade')[1].text
fb = unicodedata.normalize('NFC', fb)
fb = re.sub('[-=+,#/\?:^$.@*\"※~&%ㆍ!』\\‘|\(\)\[\]\<\>`\'…》🔥🔥👏😮😢🔥]','',fb).strip()
fb
>> soup.select('div._a0zs > span._aacl._~~')
: div 태그의 class명이 _a0zs인 것의 바로 아래에 있는 span 태그의 class _aacl._ ~ 선택
[1] : 두 번째 태그를 선택
text : 해당 태그의 텍스트
>> unicodedata.normalize : 한글 자음/모음 분리되는 현상 방지
>> re.sub() 안에 있는 특수문자들 제거하고
.strip() : 양 쪽 공백 제거
rp_f = []
replies = soup.select('div._a9zs > span._aacl._aaco._aacu._aacx._aad7._aade')
for reply in replies:
rp_a = reply.text.strip()
rp_a = re.sub('[-=+,#/\?:^$.@*\"※~&%ㆍ!』\\‘|\(\)\[\]\<\>`\'…》🔥🔥👏😮😢😍😂🥴🙌🤤😋👍🧭💛💚]','',rp_a).strip()
rp_f.append(rp_a)
print(rp_f)
print()
feedback = rp_f[1:]
feedback
>> rp_f 빈 리스트를 만들고
soup.select() 안의 정보를 replies에 넣고
>> for문을 돌면서 해당 텍스트를 양쪽 제거한 정보를 rp_a라고 하고
re.sub() 안의 특수문자를 제거한 뒤 re_a 업데이트
이 것을 rp_f 리스트에 추가
- 게시글 정보 가져오기 자동화
import re
from bs4 import BeautifulSoup
import unicodedata
def get_content(driver):
# 현재 페이지 html 에서 정보 가져오기
html = driver.page_source
soup = BeautifulSoup(html, 'html.parser')
# 본문 내용 가져오기
try:
content = soup.select('div._aat6 > ul')[0].text
content = unicodedata.normalize('NFC',content)
except:
content = ' '
# 본문 내용에서 # (해시태그) 가져오기 (정규표현식 활용)
tags = re.findall(r'#[^\s#,\\]+', content)
# 작성일자 정보 가져오기
date = soup.select('time._a9ze._a9zf')[0]['datetime'][:10]
# 좋아요 수 가져오기
try:
like = soup.select('div._aacl._aaco._aacu._aacx._aad6._aade')[0].text[14:18]
except:
like = 0
# 댓글 가져오기
try:
rp_f = []
replies = soup.select('div._a9zs > span._aacl._aaco._aacu._aacx._aad7._aade')
for reply in replies:
rp_a = reply.text.strip()
rp_a = re.sub('[-=+,#/\?:^$.@*\"※~&%ㆍ!』\\‘|\(\)\[\]\<\>`\'…》🔥🔥👏😮😢😍😂🥴🙌🤤😋👍🧭💛💚]','',rp_a).strip()
rp_f.append(rp_a)
feedback = rp_f[1:]
# feedback = unicodedata.normalize('NFC', feedback)
except:
feedback = ' '
# 수집한 저장하기
data = [content, date, like, feedback, tags]
return data
get_content(driver)