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

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

개발

python BeautifulSoup4 에서 find 와 select 의 차이점

페이지 정보

profile_image
작성자 관리자 (112.♡.173.204)
댓글 0건 조회 2,022회 작성일 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 386건 4 페이지
  • RSS
개발 목록
번호 제목 조회 추천 날짜
3262566005-14
3252783005-11
3243584005-10
3232736005-08
3224486005-07
3212943005-07
3202587005-06
3197004005-06
3183092005-04
3173758005-04
3161682005-04
3151631005-04
3143152005-04
3131853005-02
3123159005-01
3112093005-01
열람중2023005-01
3092771004-30
3081837004-30
3073444004-30

검색


사이트 정보

Copyright © Baragi.Net. All rights reserved.