BeautifulSoup을 사용하여 웹 페이지를 구문 분석하고 있습니다. 이제 31811.75
범위에서 Index 값을 읽고 싶습니다 .
<span>Underlying Index: <b style="font-size:1.2em;">BANKNIFTY 31811.75</b> </span>
불행히도 스팬에는 class
. 비슷한 질문에 언급 된 솔루션을 따랐 지만 전체 텍스트를 얻지 못하는 것 같습니다.
>>> print(soup.body(text=re.compile('Underlying')))
['Underlying Index: ']
키워드 Underlying
를 사용 하여 범위에있는 텍스트를 추출하고 싶습니다 . 어떻게 할 수 있습니까?
찾고 싶지 않은 범위 가있는 합성 HTML 문서를 만들었습니다 . 다음을 사용하여 찾은 텍스트에서 소수 추출re.findall()
from bs4 import BeautifulSoup
import re
html = """
<html><body>
<span>unwanted</span>
<span>Underlying Index: <b style="font-size:1.2em;">BANKNIFTY 31811.75</b> </span>
</html></body>
"""
soup = BeautifulSoup(html)
index = re.findall("\d+\.\d+", soup.find(lambda tag:tag.name=="span" and "Underlying" in tag.text).text )
index[0] if len(index)==1 else None # re.findall() returns a list, take first located decimal. Could default to 0.0 instead of None
'31811.75'
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다