首页 > 解决方案 > python中的抓取表

问题描述

有人可以帮我从https://www.statsinsider.com.au/prediction-results?fbclid=IwAR18wxeCq_ygxLG1v2JEe3YqBNNS6krzNnOQULYp4IZihQY6JMgHwzpIl6o上的大表中抓取数据

我在这里有一些基础:

from bs4 import BeautifulSoup
from requests_html import HTMLSession
session = HTMLSession()
url = 'https://www.statsinsider.com.au/prediction-results?fbclid=IwAR18wxeCq_ygxLG1v2JEe3YqBNNS6krzNnOQULYp4IZihQY6JMgHwzpIl6o'
r = session.get(url)
soup=BeautifulSoup(r.html.html,'html.parser')
stat_table = soup.find('table')

这会输出以下内容,这似乎不是整个表格。帮助表示赞赏,谢谢!

<table>
<tbody>
<tr>
<th>Date</th>
<th class="to-hide">Sport</th>
<th>Team</th>
<th class="to-hide">Bet Type</th>
<th>Odds</th>
<th class="to-hide">Bet</th>
<th>Result</th>
<th>Profit/Loss</th>
</tr>
<tr ng-repeat="match in recentResults">
<td>{{match.Date}}</td>
<td class="to-hide">{{match.Sport}}</td>
<td>{{match.Team}}</td>
<td class="to-hide">{{match.Type}}</td>
<td>${{match.Odds}}</td>
<td class="to-hide">${{match.Bet}}</td>
<td>{{match.Result}}</td>
<td class="green" ng-if="match.Return &gt; 0">${{match.Return}}</td>
<td class="red" ng-if="match.Return &lt; 0">${{match.Return}}</td>
<td ng-if="match.Return == 0"></td>
</tr>
</tbody>
</table>

标签: pythonweb-scrapingbeautifulsoupscrapy

解决方案


该表是使用 AJAX 调用动态创建的。

该页面正在获取 3 个 JSON 文档 - 其中之一是您要查找的文档。

  1. https://gazza.statsinsider.com.au/results.json?sport=NFL
  2. https://gazza.statsinsider.com.au/sportladder.json?sport=nba
  3. https://gazza.statsinsider.com.au/upcoming.json

您需要做的就是对上面的每个 URL 进行 HTTP GET 并检查其中哪一个是表格模式。找到正确的 URL 后,使用请求并获取数据。


推荐阅读