尝试使用BeautifulSoup抓取NFL统计表的某些部分

时间:2015-11-30 19:45:13

标签: python html python-3.x beautifulsoup

我想抓住桌子上的每个特定数据。我已将其缩小到团队的每一栏,只需抓住实际数字即可!我的代码是:

import requests
from bs4 import BeautifulSoup
url = 'http://espn.go.com/nfl/statistics/team/_/stat/defense/position/defense'
response = requests.get(url)
html = response.content
soup = BeautifulSoup(html)
HoustonDefense = soup.find('tr', class_="oddrow team-28-34")
print (HoustonDefense.prettify())

这将打开HoustonDefense列:

<tr align="right" class="oddrow team-28-34">
  <td align="left">
    1
  </td>
  <td align="left">
    <a href="http://espn.go.com/nfl/team/_/name/hou/houston-texans">
   Houston
  </a>
  </td>
  <td>
    539
  </td>
  <td>
    272
  </td>
  <td class="sortcell">
    811
  </td>
  <td>
    22.0
  </td>
  <td>
    136
  </td>
  <td>
    65
  </td>
  <td>
    9
  </td>
  <td>
    102
  </td>
  <td>
    38
  </td>
  <td>
    1
  </td>
  <td>
    17
  </td>
  <td>
    5
  </td>
  <td>
    2
  </td>
</tr>

我想在每个<td></td>之间抓取这些数字并将它们分配给变量。任何帮助都会很棒!谢谢!

1 个答案:

答案 0 :(得分:1)

使用td查找tr中的所有td元素,并获取除前两个单元格(排名和团队名称本身)之外的每个[td.text for td in HoustonDefense.find_all("td")[2:]] 的文本):

[u'539', u'272', u'811', u'22.0', u'136', u'65', u'9', u'102', u'38', u'1', u'17', u'5', u'2']

打印:

 SET @query = (N'SELECT [' +@outColumn+'] FROM [Production].[dbo].[v_time] WHERE textile LIKE '''+@outTextile+'''')