【问题标题】:python- is beautifulsoup misreporting my html?python-beautifulsoup 是否误报了我的 html?
【发布时间】:2010-10-23 01:11:30
【问题描述】:

据我所知,我各有两台机器,运行 python 2.5 和 BeautifulSoup 3.1.0.1。

我正在尝试抓取http://utahcritseries.com/RawResults.aspx,使用:

from BeautifulSoup import BeautifulSoup
import urllib2

base_url = "http://www.utahcritseries.com/RawResults.aspx"

data=urllib2.urlopen(base_url)
soup=BeautifulSoup(data)
i = 0
table=soup.find("table",id='ctl00_ContentPlaceHolder1_gridEvents')
#table=soup.table
print "begin table"
for row in table.findAll('tr')[1:10]:
    i=i + 1
    col = row.findAll('td')
    date = col[0].string
    event = col[1].a.string
    confirmed = col[2].string
    print '%s - %s' % (date, event)
print "end table"
print "%s rows processed" % i

在我的 Windows 机器上,我得到了正确的结果,即日期和事件名称的列表。在我的mac上,我没有。相反,我得到了

3/2/2002 - Rocky Mtn Raceway Criterium
None - Rocky Mtn Raceway Criterium
3/23/2002 - Rocky Mtn Raceway Criterium
None - Rocky Mtn Raceway Criterium
4/2/2002 - Rocky Mtn Raceway Criterium
None - Saltair Time Trial
4/9/2002 - Rocky Mtn Raceway Criterium
None - DMV Criterium
4/16/2002 - Rocky Mtn Raceway Criterium

我注意到的是,当我

print row

在我的 windows 机器上,tr 数据看起来与源 html 完全相同。请注意第二个表格行上的样式标记。这是前两行:

<tr>
<td>
 3/2/2002
</td>
<td>
 <a href="Event.aspx?id=226">
  Rocky Mtn Raceway Criterium
 </a>
</td>
<td>
 Confirmed
</td>
<td>
 <a href="Event.aspx?id=226">
  Points
 </a>
</td>
<td>
 <a disabled="disabled">
  Results
 </a>
</td>
</tr>

<tr style="color:#333333;background-color:#EFEFEF;">
<td>
 3/16/2002
</td>
<td>
 <a href="Event.aspx?id=227">
  Rocky Mtn Raceway Criterium
 </a>
</td>
<td>
 Confirmed
</td>
<td>
 <a href="Event.aspx?id=227">
  Points
 </a>
</td>
<td>
 <a disabled="disabled">
  Results
 </a>
</td>
</tr>

在我的 Mac 上,当我打印前两行时,样式信息会从 tr 标签中删除,并移到每个 td 字段中。我不明白为什么会这样。每隔一个日期值我都会得到 None ,因为 BeautifulSoup 每隔一个日期就放置一个字体标签。这是mac的输出:

<tr>
<td>
 3/2/2002
</td>
<td>
 <a href="Event.aspx?id=226">
  Rocky Mtn Raceway Criterium
 </a>
</td>
<td>
 Confirmed
</td>
<td>
 <a href="Event.aspx?id=226">
  Points
 </a>
</td>
<td>
 <a disabled="disabled">
  Results
 </a>
</td>
</tr>

<tr bgcolor="#EFEFEF">
<td>
 <font color="#333333">
  3/16/2002
 </font>
</td>
<td>
 <font color="#333333">
  <a href="Event.aspx?id=227">
   Rocky Mtn Raceway Criterium
  </a>
 </font>
</td>
<td>
 <font color="#333333">
  Confirmed
 </font>
</td>
<td>
 <font color="#333333">
  <a href="Event.aspx?id=227">
   Points
  </a>
 </font>
</td>
<td>
 <font color="#333333">
  <a disabled="disabled">
   Results
  </a>
 </font>
</td>
</tr>

我的脚本在 windows 下显示正确的结果 - 我需要做什么才能让我的 Mac 正常工作?

【问题讨论】:

  • 您是说 HTML 因您的客户端浏览器而异?您是说该站点为 Windows 机器提供的 HTML 与为您的 Mac 提供的 HTML 不同吗?顺便说一句,这并不罕见。
  • 不,我是说打印表的输出在两台不同的机器上看起来不同。在浏览器中,html 看起来是一样的。我真的不知道如何验证我实际使用的 beautifulSoup 版本。在我的 Mac 上,/Library/Python/2.5/site-packages 中有一个 BeautifulSoup 3.1 .egg

标签: python macos configuration screen-scraping beautifulsoup


【解决方案1】:

我怀疑问题出在 urllib2 请求中,而不是 BeautifulSoup:

如果您向我们展示与此命令在两台机器上返回的原始数据的相同部分,这可能会有所帮助:

urllib2.urlopen(base_url)

此页面看起来可能会有所帮助: http://bytes.com/groups/python/635923-building-browser-like-get-request

最简单的解决方案可能只是检测脚本在哪个环境中运行并相应地更改解析逻辑。

>>> import os
>>> os.uname() 
('Darwin', 'skom.local', '9.6.0', 'Darwin Kernel Version 9.6.0: Mon Nov 24 17:37:00 PST 2008; root:xnu-1228.9.59~1/RELEASE_I386', 'i386')

或者让微软使用网络标准:)

另外,你没有使用 mechanize 来获取页面吗?如果是这样,问题可能就在那里。

【讨论】:

    【解决方案2】:

    BeautifulSoup 3.1 版中有documented problems

    您可能需要仔细检查您实际使用的版本,如果是,请降级。

    【讨论】:

      猜你喜欢
      • 2010-10-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-07-12
      • 1970-01-01
      • 2017-04-06
      • 2016-05-09
      • 1970-01-01
      相关资源
      最近更新 更多