새소식

ETC/Tip - 2023.02.21

[BS4] select()와 find()의 차이점

  • -

select()

  • CSS 선택자를 활용해서 HTML 태그를 찾는 방식
    • 더 다양한 조건을 활용해 직관적으로 찾을 수 있음  
titles = soup.select("div.cont_thumb > p.txt_thumb")
for title in titles:
    if title is not None:
        print(title.text)

find()

  • HTML 태그를 직접 찾는 방식
cont_thumb = soup.find_all("div", "cont_thumb")
for cont in cont_thumb:
    title = cont.find("p", "txt_thumb")
    if title is not None:
        print(title.text)

참고

 

BeautifulSoup 모듈 find와 select의 차이점 - 복잡한 웹을 간단하게

BeautifulSoup은 HTML 문서를 예쁘게 정돈된 파스트리로 변환하여 내놓는 파이썬 라이브러리다. 이 잘 정돈된 데이터 구조는 Beautiful Soup 객체로서 여러 tag 객체로 이루어져 있다. 영어, 한국어와 같은

desarraigado.tistory.com

 

Contents

포스팅 주소를 복사했습니다

이 글이 도움이 되었다면 공감 부탁드립니다.