【发布时间】:2014-03-31 11:29:30
【问题描述】:
我一直在尝试从该站点 (http://nflcombineresults.com/nflcombinedata.php?year=1999&pos=WR&college=) 抓取数据的不同方法,但似乎无法让其中任何一种工作。我尝试过使用给定的索引,但似乎无法使其工作。我想我在这一点上尝试了太多东西,所以如果有人能指出我正确的方向,我会非常感激。
我想提取所有信息并将其导出为 .csv 文件,但此时我只是想获取要打印的名称和位置以开始使用。
这是我的代码:
import urllib2
from bs4 import BeautifulSoup
import re
url = ('http://nflcombineresults.com/nflcombinedata.php?year=1999&pos=&college=')
page = urllib2.urlopen(url).read()
soup = BeautifulSoup(page)
table = soup.find('table')
for row in table.findAll('tr')[0:]:
col = row.findAll('tr')
name = col[1].string
position = col[3].string
player = (name, position)
print "|".join(player)
这是我得到的错误: 第 14 行,在 name = col[1].string IndexError: 列表索引超出范围。
--更新--
好的,我已经取得了一些进展。它现在允许我从头到尾进行,但它需要知道表中有多少行。我怎样才能让它通过它们直到最后? 更新代码:
import urllib2
from bs4 import BeautifulSoup
import re
url = ('http://nflcombineresults.com/nflcombinedata.php?year=1999&pos=&college=')
page = urllib2.urlopen(url).read()
soup = BeautifulSoup(page)
table = soup.find('table')
for row in table.findAll('tr')[1:250]:
col = row.findAll('td')
name = col[1].getText()
position = col[3].getText()
player = (name, position)
print "|".join(player)
【问题讨论】:
标签: python-2.7 html-parsing web-scraping beautifulsoup