파이썬 데이터 분석

파이썬 크롤링 코드

병통 2021. 12. 29. 23:48

파이썬을 활용한 크롤링 코드

html을 가져와서 필요한 부분 추출 진행 중

21.12.28

#네이버 블로그 제목 크롤링 소스 
from bs4 import BeautifulSoup
import requests

type(pageurl)
soup = BeautifulSoup(pageurl.content, 'html.parser')
type(soup)
result = soup.find_all(attrs = {'class':'se-main-container'})

for i in result:
    print(i.get_text())
    print(i.attrs['href'])
 
 
 
해당 부분 실행 시 가져와지긴 하지만 전체가 나오지 않음
 
이를 해결하기 위해 drive를 활용한 방법을 실시할 예정
 
 

#--------------------------

21.12.29

import os
from selenium import webdriver
driver = webdriver.Chrome(os.path.abspath('chromedriver'))
driver.switch_to.frame('mainFrame')
html = driver.page_source
print(html)
driver.close()

실행은 된다만 그 이후의 가공이 진행되지 않는 상태
 즉 필요한 부분에 대한 추출이 이루어지지 않고 있다.

import os
from selenium import webdriver
driver = webdriver.Chrome(os.path.abspath('chromedriver'))
driver.switch_to.frame('mainFrame')
html = driver.page_source
print(html)

result = html.find_all(attrs = {'class':'se-main-container'})
result = html(attrs = {'class':'se-main-container'})
for i in result:
    print(i.get_text())
    print(i.attrs['href'])

driver.close()

혹시 몰라서 그대로 붙여넣기한 result 부분.
역시 실행 방법이 다르다 보니 오류가 발생한다.

해결하기 위해 계속해서 노력해나갈 예정

#----------------------------------------------------

21.12.31

#네이버 블로그 제목 크롤링 소스 

from bs4 import BeautifulSoup
import requests

type(pageurl)
soup = BeautifulSoup(pageurl.content, 'html.parser')
type(soup)
result = soup.find_all(attrs = {'class': 'api_txt_lines total_tit _cross_trigger})

for i in result:
    print(i.get_text())
    print(i.attrs['href'])
 
 
일단은 블로그 제목과 url을 추출하는 방법은 성공했다.
하지만 네이버 블로그 채널 안에서 진행하면 페이지 수를 클릭해서 진행하지만,
(https://section.blog.naver.com/Search/Post.naver?pageNo=1&rangeType=ALL&orderBy=sim&keyword=파이썬)
 
네이버 view를 사용해서 보면 스크롤을 내려야 다음 게시글이 차례로 보이기 때문에 더 많고 다양한 블로그를 가져올 수 없다. (https://search.naver.com/search.naver?where=view&sm=tab_jum&query=파이썬)
 

더 많은 내용을 탐색하고 싶지만 이때까지 찾아본 내용으로는 내가 충족할만한 데이터의 수준을 수집할 수 없었다.
 
좀 더 알아볼 필요가 있을 듯 하다.