gpt4 book ai didi

python - 基于跨度文本的抓取站点

转载 作者:行者123 更新时间:2023-12-01 03:51:19 24 4
gpt4 key购买 nike

我正在尝试从以下网站仅抓取“MLB”分数:www.scoresandodds.com/pgrid_20160628.html?sort=rot

HTML 代码的一部分如下所示:

<div xmlns:dat="http://scoresandodds.com/dataset-main" class="section">
<div class="heading"><
span class="league">MLB</span>
<span class="date">06/28/2016</span>
</div><table cellpadding="0" summary="" cellspacing="0" border="0"><thead>
<tr><th class="first">Team</th><th>Pitcher</th><th>Open</th><th>Current</th><th>Runline</th><th>Scores</th><th>Notes</th>
</tr></thead><tbody><tr><td class="teamName">901 <a href="http://scoresandodds.com/statfeed/statfeed.php?page=MLB/MLBteam&amp;teamid=NY+METS&amp;season=">NEW YORK METS</a></td>
<td class="pitcher">(r) harvey, m</td>
<td class="line">8</td>
<td class="line">8.5o15</td>
<td class="line">+1.5(-200)</td>
<td class="score">0 Under 8.5</td>

我的代码开头为:

url = "http://www.scoresandodds.com/pgrid_"+date+".html?sort=rot"
soup = BeautifulSoup(urllib2.urlopen(url), 'html.parser')
scores = soup.find_all("span", {"class": "league"})

print(scores)
[<span class="league">MLB</span>, <span class="league">WNBA</span>]

它返回的结果很棒,但我不清楚如何仅抓取“MLB”分数的数据。

最佳答案

找到MLB“标签”并仅通过 find_next() 获取下面的第一个表格 :

mlb_table = soup.find("span", class_="league", text="MLB").find_next("table")

关于python - 基于跨度文本的抓取站点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38206927/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com