【问题标题】:Scraping table in pythonpython中的抓取表
【发布时间】:2019-08-14 17:54:06
【问题描述】:

谁能帮我从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>

【问题讨论】:

  • 你能详细说明一下它看起来不像我习惯的普通简单表吗? 究竟你不能在这里做什么?
  • @KeyurPotdar 该表似乎没有类,并且有像ng-repeat="match in recentResults"这样的奇怪属性

标签: python web-scraping beautifulsoup scrapy


【解决方案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>

【讨论】:

  • 您好,谢谢您的回答!请查看我按照您的建议完成的更新问题。
  • @Programmer 我能够将整个表作为输出。但是也可以在代码中尝试这个r.html.render(sleep=5) 而不是r.html.render()
  • 我实际上不得不删除那部分代码,因为我收到错误 Cannot use HTMLSession within an existing event loop. Use AsyncHTMLSession instead. 我在 jupyter 中运行,也许这就是原因?
  • @Programmer 如果没有该部分,代码将无法工作。正是那条线呈现页面。我正在从我的控制台而不是从 jupyter 尝试。你能在 jupyter 外面试试吗?
  • @Bitto Bennichan 我遇到了同样的问题Cannot use HTMLSession within an existing event loop. Use AsyncHTMLSession instead.如何解决这个问题?
【解决方案2】:

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

【讨论】:

    猜你喜欢
    • 2021-04-20
    • 1970-01-01
    • 2013-09-28
    • 1970-01-01
    • 2021-04-26
    • 2018-07-02
    • 1970-01-01
    • 2015-02-04
    • 2015-01-29
    相关资源
    最近更新 更多