【问题标题】:How to extract webdata with Python, BeautiflSoup and mechanize from table如何使用 Python、BeautifulSoup 和 mechanize 从表中提取 Web 数据
【发布时间】:2011-08-15 23:57:27
【问题描述】:

我想从这个网站的表格中提取数据:http://www.pgatour.com/r/stats/info/xm.html?101 然后将其另存为 .csv 并带入 iWorks Numbers 表。 我一直在尝试使用 Python、BeautifulSoup 和 mechanize。通过查看其他示例,我一直在尝试没有知识,但没有成功。我已经走到这一步了:

from BeautifulSoup import BeautifulSoup, SoupStrainer
from mechanize import Browser
import re
br = Browser()
response = br.open("http://www.pgatour.com/r/stats/info/xm.html?101").read()

然后我用firebug查看代码,我猜我需要解析<tbody></tbody>之间的数据。但我不知道该怎么做。 非常感谢任何帮助。

【问题讨论】:

  • 如果您的问题已经得到解答,请点击接受。

标签: python beautifulsoup mechanize


【解决方案1】:

在主页中,旅游统计数据似乎由 JavaScript <div class="tourViewData"> ... populateDDs(); 填充 BS 不解析 Javascript,请参阅许多其他 SO 问题。 (我不知道如何解决该部分。最坏的情况是,选择该 HTML 选择并将其保存为本地 html 文件,作为一种解决方法。)

首先,将 s 设置为该 URL 的 BeautifulSoup 对象(我使用斜纹而不是原始机械化,将您的机械化等效物放在这里):

from BeautifulSoup import BeautifulSoup, SoupStrainer
#from mechanize import Browser
from twill.commands import *
import re

go("http://www.pgatour.com/r/stats/info/xm.html?101")
s = BeautifulSoup(get_browser().get_html())

无论如何,您要查找的统计数据表是带有<tbody><tr class="tourStatTournHead"> 标记的表。 只是为了让事情有点古怪,其行中的标签属性交替定义为<tr class="tourStatTournCellAlt"<tr class=""...。 我们应该搜索第一个<tr class="tourStatTournCellAlt",然后处理之后表中的每个<tr>,除了标题行(<tr class="tourStatTournHead">)。

遍历行:

tbl = s.find('table', {'class':'tourStatTournTbl'})

def extract_text(ix,tg):
    if ix==2: # player name field, may be hierarchical
        tg = tg.findChildren()[0] if (len(tg.findChildren())>0) else tg
    return tg.text.encode()

for rec in tbl.findAll('tr'): # {'class':'tourStatTournCellAlt'}):
    # Skip header rows
    if (u'tourStatTournHead' in rec.attrs[0]):
        continue        
    # Extract all fields
    (rank_tw,rank_lw,player,rounds,avg,tot_dist,tot_drives) = \
        [extract_text(i,t) for (i,t) in enumerate(rec.findChildren(recursive=False))]
    # ... do stuff

我们为玩家名称添加了一个辅助函数(它可能是分层的,也可能不是分层的,如果它嵌入了 Titleist 标志。) 可能您想将大多数字段转换为 int() 除了 player(string) 和 avg(float);如果是这样,请记住从排名字段中删除可选的“T”(表示并列),并从 tot_dist 中删除逗号。

【讨论】:

  • 感谢您的努力和时间!尝试输入您的代码时出现此错误:
  • 感谢您的努力和时间!尝试输入您的代码时出现此错误: >>> tbl = s.find('table', {'class':'tourStatTournTbl'}) Traceback (last recent call last): File "", line 1 ,在 NameError: name 's' is not defined 我猜你的代码很好,是我做错了什么。我不想再浪费你的时间了。我认为在尝试此操作之前我需要正确学习 Python。我将复制您的代码并研究更多python,然后再试一次。非常感谢!!!
  • s 应该是该 URL 的 BeautifulSoup 对象。 (我用斜纹布而不是机械化。)
猜你喜欢
  • 2013-01-29
  • 1970-01-01
  • 2012-04-04
  • 1970-01-01
  • 1970-01-01
  • 2019-05-24
  • 1970-01-01
  • 2021-04-28
  • 1970-01-01
相关资源
最近更新 更多