python BeautifulSoup4 에서 find 와 select 의 차이점 > 개발

본문 바로가기
사이트 내 전체검색

개발

python BeautifulSoup4 에서 find 와 select 의 차이점

페이지 정보

profile_image
작성자 관리자 (112.♡.173.204)
댓글 0건 조회 2,320회 작성일 21-05-01 15:56

본문

python bs4 에서 요소를 찾을 때 사용하는 method 는 크게 2가지가 있다. find 와 select.
이 둘을 비교해 본다.


## find vs select

기본적으로 대응되는 함수는 다음과 같다.
find <-> select_one
find_all <-> select

즉,
find 는 하나만 찾고, find_all 이 모두를 찾는다.
select 는 모두를 찾고, select_one 는 하나만 찾는다.

method 이름에서 뭔가 이유를 찾을 수 있을 것 같기도 하다.


## 속도 비교

속도를 비교해 놓은 실험이 있다.
- 출처 : https://stackoverflow.com/questions/38028384/beautifulsoup-difference-between-find-and-select
```
from bs4 import BeautifulSoup
from glob import iglob

def parse_find(soup):
    author = soup.find("h4", class_="h12 talk-link__speaker").text
    title = soup.find("h4", class_="h9 m5").text
    date = soup.find("span", class_="meta__val").text.strip()
    soup.find("footer",class_="footer").find_previous("data", {
        "class": "talk-transcript__para__time"}).text.split(":")
    soup.find_all("span",class_="talk-transcript__fragment")

def parse_select(soup):
    author = soup.select_one("h4.h12.talk-link__speaker").text
    title = soup.select_one("h4.h9.m5").text
    date = soup.select_one("span.meta__val").text.strip()
    soup.select_one("footer.footer").find_previous("data", {
        "class": "talk-transcript__para__time"}).text
    soup.select("span.talk-transcript__fragment")

def  test(patt, func):
    for html in iglob(patt):
        with open(html) as f:
            func(BeautifulSoup(f, "lxml")
```


## 결론

select 가 문장도 더 간결해 보이고, 속도가 빠르다고 한다.



## 참고
https://stackoverflow.com/questions/38028384/beautifulsoup-difference-between-find-and-select

추천0

댓글목록

등록된 댓글이 없습니다.

Total 392건 5 페이지
  • RSS
개발 목록
번호 제목 조회 추천 날짜
3123517005-01
3112364005-01
열람중2321005-01
3093018004-30
3082160004-30
3073779004-30
3063395004-29
3052450004-29
3042362004-29
3033135004-28
3022798004-28
3014289004-28
3002200004-25
2993434004-22
2982165004-14
2972710004-14
2962675004-12
2953724004-11
2942501004-07
2933347004-07

검색


사이트 정보

Copyright © Baragi.Net. All rights reserved.