在Python3中仅选择此数据中的统计信息

时间:2016-03-16 09:06:56

标签: python web-scraping beautifulsoup python-requests python-3.5

我试图从此链接中抓取数据:http://www.hoopsstats.com/basketball/fantasy/nba/opponentstats/16/12/eff/1-1

我的目标是能够将此数据导入Excel。

我在代码中已经走到了尽头:

import sys
import requests
from bs4 import BeautifulSoup

r = requests.get('http://www.hoopsstats.com/basketball/fantasy/nba/opponentstats/16/12/eff/1-1')


soup = BeautifulSoup(r.text, "html.parser")

stats = soup.find_all('table', 'statscontent')


print(stats)

这是返回的内容的开始:

[<table bgcolor="#EBE9E9" cellpadding="0" cellspacing="0" class="statscontent" height="20" width="100%">
<tr id="myid1/0" onmouseout="hide_table_effect('myid1/0')" onmouseover="show_table_effect('myid1/0')">
<td width="3%"><center>1</center></td>
<td align="left" width="9%"><a href="/basketball/fantasy/nba/boston-celtics/team/profile/16/2">Boston</a></td>
<td width="3%"><center>66</center></td>
<td width="4%"><center>48.0</center></td>
<td width="4%"><center>19.6</center></td>
<td width="4%"><center>5.2</center></td>
<td width="4%"><center>7.2</center></td>
<td width="4%"><center>1.8</center></td>
<td width="4%"><center>0.5</center></td>
<td width="4%"><center>4.3</center></td>
<td width="4%"><center>4.1</center></td>
<td width="4%"><center>4.3</center></td>
<td width="4%"><center>0.9</center></td>
<td width="8%"><center>6.8-16.2</center></td>
<td width="3%"><center>.423</center></td>
<td width="7%"><center>1.6-5.0</center></td>
<td width="3%"><center>.324</center></td>
<td width="8%"><center>4.3-5.3</center></td>
<td width="3%"><center>.818</center></td>
<td width="5%"><center>19.8</center></td>
<td width="4%"><center>-6.7</center></td>
</tr>
</table>, <table bgcolor="#F8F8F8" cellpadding="0" cellspacing="0" class="statscontent" height="20" width="100%">
<tr id="myid1/1" onmouseout="hide_table_effect('myid1/1')" onmouseover="show_table_effect('myid1/1')">
<td width="3%"><center>2</center></td>
<td align="left" width="9%"><a href="/basketball/fantasy/nba/san-antonio-spurs/team/profile/16/27">San Antonio</a></td>
<td width="3%"><center>66</center></td>
<td width="4%"><center>47.9</center></td>
<td width="4%"><center>19.6</center></td>
<td width="4%"><center>5.0</center></td>
<td width="4%"><center>8.7</center></td>
<td width="4%"><center>1.8</center></td>
<td width="4%"><center>0.3</center></td>

我需要&#39; x&#39;之间的数字。

也最好将数据格式化,以便可以在CSV文件中轻松使用

1 个答案:

答案 0 :(得分:0)

这似乎可以解决问题:

    capabilities.setCapability("version", "5.1");
    capabilities.setCapability("deviceName", "SomethingDoesntMatter");
    capabilities.setCapability("browserName", "chrome");
    capabilities.setCapability("device", "Android");
    capabilities.setCapability("udid","ZX1D63R33N");   // 192.168.56.101:5555   // ZX1D63R33N
    capabilities.setCapability("platform", "Android");

可能多余或有其他缺陷,但我得到了我想要的东西