ABOUT ME

Today
Yesterday
Total
  • 뽐뿌게시판 컴퓨터 카테고리 제목 크롤링해서 자동으로 가져오기
    기타 2019. 11. 27. 20:23

    블프 시즌을 맞아 컴퓨터를 사려고 뽐뿌를 계속 들어가는데, 직접 일일히 확인하기가 너무 귀찮아서 크롤링 프로그램을 간단하게 만들었다. 이왕 만든김에 포스팅 하려고 두 가지 버전으로 만들었다.

     

    1. BeautifulSoup

    스크래핑을 하려는 페이지가 간단할 때 쓰면 좋다. html 문서를 들고와서 그 문서를 분석해서 필요한 내용을  찾을 수 있다.

    from bs4 import BeautifulSoup
    import requests
    
    # 바로 뽐뿌 컴퓨터 게시판으로 접속한다
    r = requests.get('http://www.ppomppu.co.kr/zboard/zboard.php?id=ppomppu&category=4')
    if r.status_code == 200:
    	# html을 들고와서
        bs = BeautifulSoup(r.text, 'html.parser')
    
    	# 필요한 부분을 가져온다
        titles = bs.find_all('font', class_ = 'list_title')
        for title in titles:
            print(title.string)
    
    print('===========================================================================================')
    
    # 여기는 해외 뽐뿌.. 내용은 동일
    r = requests.get('http://www.ppomppu.co.kr/zboard/zboard.php?id=ppomppu4&category=3')
    if r.status_code == 200:
        bs = BeautifulSoup(r.text, 'html.parser')
    
        titles = bs.find_all('font', class_ = 'list_title')
        for title in titles[1:]:
            print(title.string)

     

     

    2. selenium

    스크래핑 하려는 페이지가 복잡하더라도, 실제 human이 작업하는 것처럼 필요한 내용을 찾아낼 수 있다. 이를 위해 사용하려는 드라이버를 미리 받아야 한다.

     

    from selenium import webdriver
    
    # 구글드라이버는 미리 준비해야 한다
    driver = webdriver.Chrome('./chromedriver')
    
    try:
    	# 크롬으로 뽐뿌 게시판에 접속해서
        driver.get('http://www.ppomppu.co.kr/zboard/zboard.php?id=ppomppu&category=4')
        
        # 필요한 부분을 찾는다
        table = driver.find_element_by_id('revolution_main_table')
        boxes = driver.find_elements_by_xpath(".//*[@class='list_title']")
    
    	#출력하면 끝!
        for box in boxes:
            print(box.text)
    
    except Exception as e:
        print(e)
    
    finally:
        driver.close()
    

    댓글

Designed by Tistory.