컴퓨터/Python

python selenium naver cafe 게시글 크롤링 pyinstaller

풍경소리^^ 2020. 10. 11. 13:12

www.youtube.com/watch?v=jg5Ph2s4dUw

Code Printer

=========================

from selenium import webdriver

from bs4 import BeautifulSoup

import sys

import os

path="C:\Python\webdriver\chromedriver\chromedriver.exe"

driver=webdriver.Chrome(path)

if getattr(sys, 'frozen',False):

    chromedriver_path=os.path.join(sys._MEIPASS,"chromedriver.exe")

    driver=webdriver.Chrome(chromedriver_path)

else:

    driver=webdriver.Chrome(path)

driver.get("https://www.naver.com/")

 

base_url="https://cafe.naver.com/fingerstyle"

url="/ArticleList.nhn?search.clubid=10174263&search.menuid=22&search.boardtype=L"

 

driver.get(base_url+url)

driver.switch_to.frame('cafe_main')

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

soup=soup.find_all(class_='article-board m-tcol-c')[1]

rows=soup.find_all('td'class_='td_article')

for row in rows:

    article_title=row.find('a',class_='article')

    # span요소를 제거

    if len(article_title.find_all('span'))>0:

        for s in soup('span'):

            s.extract()

    article_title=article_title.get_text().strip()

    print(article_title)

driver.close()

=================================

c:\ 에 chromedriver.exe 넣고

 

별도 cmd 창에서 실행

pyinstaller --onefile --add-binary "C:\chromedriver.exe";"." main.py