我一直在嘗試使用 BeautifulSoup從 span 中提取資料,但不知何故它拋出了一個錯誤。
HTML
<td style="text-align:right"><span class="sc-15yy2pl-0 hzgCfk"><span class="icon-Caret-down"></span>3.69<!-- -->%</span></td>
<td style="text-align:right"><span class="sc-15yy2pl-0 hzgCfk"><span class="icon-Caret-down"></span>5.33<!-- -->%</span></td>
我提取跨度資料的代碼
page_content= BeautifulSoup(http.html, 'html.parser')
content= page_content.td.contents
data= content.span.string
我想從 span 標簽中提取 3.69% 和 5.33%。
uj5u.com熱心網友回復:
使用 find_all 方法
您可以使用 find_all() 方法查找 html 的文本內容并僅使用您自己的鏈接
import requests
from bs4 import BeautifulSoup
result = requests.get(yourUrl)
src = result.content
soup = BeautifulSoup(src, "html.parser")
tds = soup.find_all("td")
contents = []
for td in tds:
content = td.find("span", {"class":"sc-15yy2pl-0 hzgCfk"}).find("span", {"class":"icon-Caret-down"}).text
contents.append(result)
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/428666.html
標籤:html python-3.x 网页抓取 美丽的汤
