在python中的刮表

问题描述 投票:1回答:2

有人可以帮我从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>
python web-scraping beautifulsoup scrapy
2个回答
1
投票

由于您已经在使用请求,因此您可能需要考虑使用Requests-HTML。虽然它的功能并不像selenium那样先进,但在这样的情况下它非常有用,你只需要渲染页面。

安装

pip install requests-html

您提供的链接中的表格可以使用Requests-HTML轻松删除

码:

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)
r.html.render()
soup=BeautifulSoup(r.html.html,'html.parser')
stat_table = soup.find('table')
print(stat_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 class="ng-scope" ng-repeat="match in recentResults">
<td class="ng-binding">17/09</td>
<td class="to-hide ng-binding">NFL</td>
<td class="ng-binding">NO</td>
<td class="to-hide ng-binding">Line</td>
<td class="ng-binding">$1.91</td>
<td class="to-hide ng-binding">$25</td>
<td class="ng-binding">LOSE</td>
<!-- ngIf: match.Return > 0 -->
<!-- ngIf: match.Return < 0 --><td class="red ng-binding ng-scope" ng-if="match.Return &lt; 0">$-25.00</td><!-- end ngIf: match.Return < 0 -->
<!-- ngIf: match.Return == 0 -->
</tr><!-- end ngRepeat: match in recentResults -->
</tbody>
</table>

1
投票

该表是使用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后,使用请求并获取数据。

© www.soinside.com 2019 - 2024. All rights reserved.