【问题标题】:Parsing HTML Tables with BS4用 BS4 解析 HTML 表格
【发布时间】: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


    【解决方案1】:

    我只用了 8 个小时左右就搞定了。学习很有趣。感谢凯文的帮助! 它现在包含将抓取的数据输出到 csv 文件的代码。接下来是获取这些数据并过滤掉某些位置....

    这是我的代码:

    import urllib2
    from bs4 import BeautifulSoup
    import csv
    
    url = ('http://nflcombineresults.com/nflcombinedata.php?year=2000&pos=&college=')
    
    page = urllib2.urlopen(url).read()
    
    soup = BeautifulSoup(page)
    table = soup.find('table')
    
    f = csv.writer(open("2000scrape.csv", "w"))
    f.writerow(["Name", "Position", "Height", "Weight", "40-yd", "Bench", "Vertical", "Broad", "Shuttle", "3-Cone"])
    # variable to check length of rows
    x = (len(table.findAll('tr')) - 1)
    # set to run through x
    for row in table.findAll('tr')[1:x]:
        col = row.findAll('td')
        name = col[1].getText()
        position = col[3].getText()
        height = col[4].getText()
        weight = col[5].getText()
        forty = col[7].getText()
        bench = col[8].getText()
        vertical = col[9].getText()
        broad = col[10].getText()
        shuttle = col[11].getText()
        threecone = col[12].getText()
        player = (name, position, height, weight, forty, bench, vertical, broad, shuttle, threecone, )
        f.writerow(player)
    

    【讨论】:

      【解决方案2】:

      由于防火墙权限,我无法运行您的脚本,但我认为问题出在这一行:

      col = row.findAll('tr')
      

      row 是一个tr 标签,您要求 BeautifulSoup 在该 tr 标签中查找所有 tr 标签。你可能打算这样做:

      col = row.findAll('td')
      

      此外,由于实际文本并不直接在 tds 中,而是隐藏在嵌套的 divs 和 as 中,因此使用 getText 方法而不是 .string 可能很有用:

      name = col[1].getText()
      position = col[3].getText()
      

      【讨论】:

      • 啊,有道理。谢谢!好的,所以我进行了您建议的更改,并且肯定会取得进展,因为它在页面上打印了大部分结果。虽然它以 Adrian Dingle 开头,而不是列中的名字,但在此之后打印了完整的列表,包括 |和位置。然后它返回此错误:文件“nfltest.py”,第 14 行,在 name = col[1].getText() IndexError: list index out of range。同样,我尝试使用索引,但似乎无法摆脱错误。是我一个人,还是这张表格式奇怪?
      猜你喜欢
      • 2018-12-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-30
      • 1970-01-01
      • 2013-01-02
      • 2014-09-03
      相关资源
      最近更新 更多