python BeautifulSoup4 에서 find 와 select 의 차이점
페이지 정보
본문
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
이 둘을 비교해 본다.
## 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
댓글목록
등록된 댓글이 없습니다.