여러 개의 범위 태그에서 문자열 내용을 추출하려고합니다. HTML 페이지의 스냅 샷은 다음과 같습니다 특히BeautifulSoup의 다중 범위 태그 내에서 콘텐츠 추출
<div class="secondary-attributes">
<span class="neighborhood-str-list">
Southeast
</span>
<address>
1234 Python Blvd S<br>Somewhere, NV 98765
</address>
<span class="biz-phone">
(555) 123-4567
</span>
</div>
, 나는 <span class="biz-phone></span>
태그 사이에 자리 잡고 전화 번호를 추출하기 위해 노력하고있어. 나는 다음과 같은 코드를 이렇게 시도 :
import requests
from bs4 import BeautifulSoup
res = requests.get(url)
soup = BeautifulSoup(res.text, "html.parser")
phone_number_results = [phone_numbers for phone_numbers in soup.find_all('span','biz-phone')]
구문 오류없이 컴파일 된 코드,하지만 확실히 나에게 내가 바라던 결과를 제공하지 않았다 내 질문이있다
['<span class="biz-phone">\n (702) 476-5050\n </span>', '<span class="biz-phone">\n (702) 253-7296\n </span>', '<
span class="biz-phone">\n (702) 385-7912\n </span>', '<span class="biz-phone">\n (702) 776-7061\n </span>', '<spa
n class="biz-phone">\n (702) 221-7296\n </span>', '<span class="biz-phone">\n (702) 252-7296\n </span>', '<span c
lass="biz-phone">\n (702) 659-9101\n </span>', '<span class="biz-phone">\n (702) 355-9445\n </span>', '<span clas
s="biz-phone">\n (702) 396-3333\n </span>', '<span class="biz-phone">\n (702) 643-9851\n </span>', '<span class="
biz-phone">\n (702) 222-1441\n </span>']
을 두 부분 :
- 프로그램을 실행할 때
span
태그가 나타나는 이유는 무엇입니까? - 어떻게 제거합니까? 문자열 편집 만 할 수는 있지만 BeautifulSoup 패키지를 최대한 활용하지는 못했을 것입니다. 더 우아한 방법이 있습니까?
참고 : 위에 표시된 HTML 코드 스 니펫이 페이지 전체에 더 있습니다. <span class="biz-phone"> (555) 123-4567 </span>
코드 (즉, 더 많은 전화 번호)가 추출되어야하는 사례가 더 많으므로 find_all()
을 사용하려고 한 이유는 무엇입니까?
미리 감사드립니다.
'phone_numbers.text' 또는 심지어'phone_numbers.text.strip()'사용 – furas
@furas, 트릭을 해줘서 고마워! – daOnlyBG