python - python中的抓取表
问题描述
我在这里有一些基础:
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 > 0">${{match.Return}}</td>
<td class="red" ng-if="match.Return < 0">${{match.Return}}</td>
<td ng-if="match.Return == 0"></td>
</tr>
</tbody>
</table>
解决方案
该表是使用 AJAX 调用动态创建的。
该页面正在获取 3 个 JSON 文档 - 其中之一是您要查找的文档。
- https://gazza.statsinsider.com.au/results.json?sport=NFL
- https://gazza.statsinsider.com.au/sportladder.json?sport=nba
- https://gazza.statsinsider.com.au/upcoming.json
您需要做的就是对上面的每个 URL 进行 HTTP GET 并检查其中哪一个是表格模式。找到正确的 URL 后,使用请求并获取数据。
推荐阅读
- javascript - 使用所有 css 将 div 内容保存为 pdf
- regex - 正则表达式匹配一个特定的字符或数字
- ios - 在 IOS 开发中,如何使用 swift 编程语言从用户相机胶卷中检索到的 gif 图像创建 GIF 文件?
- mongodb - 一个 MongoDB 深度索引会支持该深度的所有查询吗?
- python - Django:找不到这样的文件或目录
- windows - Windows Batch - 查找一行并替换为文本
- python - Django 2.2:如何在 Django Admin 中修改特定页面?
- java - 使用 printf 打印列
- jenkins - Jenkins Job 在 24 小时内检查失败的工作
- python - 如何减少循环