웹 크롤링

1. 개요
  - 웹으로 부터 데이터를 수집하는 작업을 말한다.
- 프로그램을 만들어 웹 서버에 쿼리를 보내 데이터를 요청하고, 이를 파싱해 필요한 정보를 추출하는 작업을 자동으로 하는 것이다.
- 화면을 구성하는 data format
    
  
2. 뷰티풀 소프4
2.1 설치
  - 콘다 프롬프트 실행
- 아래 명령어 실행

2.2 HTML 분석
2.2.1 find(), findAll()
  - find(), findAll()를 통해 내가 찾고자 하는 tag 정보를 가져 올 수 있다.
    
      - find() : 한 개 파싱
- findAll() : 전부 파싱
 
2.3 예제
from urllib.request import urlopen
from bs4 import BeautifulSoup
import re
pages = set()
def getLinks(pageUrl):
    global pages
    html = urlopen('https://en.wikipedia.org/' + pageUrl)
    bsObj = BeautifulSoup(html, 'html.parser')
    try:
        print(bsObj.h1.get_text())
        print(bsObj.find(id='mw-content-text').findAll('p'[0]))
        print(bsObj.find(id='ca-edit').find('span').find('a').attrs['href'])
    except:
        print('This page is missing something! No worries though!')
    for link in bsObj.findAll('a', href=re.compile('^(/wiki/)')):
        if 'href' in link.attrs:
            if link.attrs['href'] not in pages:
                newPage = link.attrs['href']
                print(newPage)
                pages.add(newPage)
                getLinks(newPage)
getLinks("")

        
      
      
      
      
  
     
    
      
    
  
댓글남기기