2009-11-09 30 views
2

BeautifulSoup newbe ...需要帮助BeautifulSoup被省略身体页的

下面是代码示例...

from mechanize import Browser 
from BeautifulSoup import BeautifulSoup 

mec = Browser() 
#url1 = "http://www.wines.com/catalog/index.php?cPath=21" 
url2 = "http://www.wines.com/catalog/product_info.php?products_id=4866" 
page = mec.open(url2) 
html = page.read() 
soup = BeautifulSoup(html) 

print soup.prettify() 

当我使用URL1我得到的页面的一个很好的转储。当我使用url2(我需要的那个)。我没有身体得到输出。

<!doctype html public "-//W3C//DTD HTML 4.01 Transitional//EN"> 
<html dir="LTR" lang="en"> 
<head> 
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
    <title> 
    2005 Jordan Cabernet Sauvignon Sonoma 2005 
    </title> 
</head> 
</html> 

任何想法?

+0

我发现它在头部后面截掉了整个东西,但我不明白为什么 - 'soup'对象中没有'body'标签。 – 2009-11-09 01:36:11

回答

2

是的。 HTML很糟糕。

步骤1a,print soup.prettify()并查看它在哪里停止正确缩进。

步骤1b(如果1a不起作用)。只需打印通过任何HTML美化的原料。我使用BBEdit来混淆美丽的汤。

仔细看看HTML。会有一些可怕的错误。错位"字符很常见。此外,CSS作为样式提供的背景图片具有不良的引号。

<tag style="background-image:url("something")"> 

请注意“不正确”的引号。你需要编写一个正则表达式来查找和修复这些。

第2步。编写一个“按摩”正则表达式和函数来解决这个问题。 阅读http://www.crummy.com/software/BeautifulSoup/documentation.html#Sanitizing%20Bad%20Data%20with%20Regexps部分了解如何创建标记按摩。

这是我经常使用

# Fix background-image:url("some URI") 
# to replace the quotes with &quote; 
background_image = re.compile(r'background-image:url\("([^"]+)"\)') 
def fix_background_image(match): 
    return 'background-image:url(&quote;%s&quote;)' % (match.group(1)) 
# Fix <img src="some URI name="someString""> -- note the out-of-place quotes 
bad_img = re.compile(r'src="([^ ]+) name="([^"]+)""') 
def fix_bad_img(match): 
    return 'src="%s" name="%s"' % (match.group(1), match.group(2)) 
fix_style_quotes = [ 
    (background_image, fix_background_image), 
    (bad_img, fix_bad_img), 
] 
+0

在头后立即停止任何内容...! - ) – 2009-11-09 01:44:35

0

运行有问题的HTML一个validator显示116个错误 - 太多的追查其中一个BeautifulSoup被证明无法从恢复,我想:-(

html5lib似乎能够在解析这个恐怖页面的过程中幸存下来,并留下了很多东西(美化只是关于所有原始页面,在我看来,当您使用html5lib的解析器来生成BeautifulSoup对象)。很难说如果由此产生的分析树会做你所需要的,因为我们不是真的知道什么是;-)。

注意:我已经从hg克隆源(从html5lib/python目录中只有python setup.py install)安装了html5lib,因为上一次正式发布有点长。

+0

html5lib似乎是BeautifulSoup的创造者想要我们追求的路线。他确实说过“......目前它比SGMLParser或HTMLParser要慢”,但不过推荐它作为另一种选择,因为他“不再享受美丽汤的工作,但有太多人依赖于它让项目死掉只是因为它取决于从Python标准库中删除的代码“:http://www.crummy.com/software/BeautifulSoup/3.1-problems.html – 2009-11-09 01:50:35

1

HTML确实很可怕:-) BeautifulSoup 3.0.7在处理格式不正确的HTML比当前版本更好。该项目网站警告说:“目前3.0.x系列在解析不好的HTML时比3.1系列更好”......并且有一个很棒的页面devoted to the reason why,归结为在Python 3中删除了SGMLParser,而BS 3.1.x被编写为可转换为Py3k。

好消息是,你仍然可以下载3.0。7A(最新版本),这在我的机器上解析您完美提到的网址:http://www.crummy.com/software/BeautifulSoup/download/3.x/

2

似乎被这个坏标签是越来越绊倒:

<META NAME="description" CONTENT="$49 at Wines.com "Deep red. Red- and blackcurrant, cherry and menthol on the nose, with subtle vanilla, cola and tobacco notes adding complexity. Tightly wound red berry and bitter cherry flavors are framed by dusty..."> 

显然,在这里,他们都未能逃脱在属性值内部引用(呃 - 哦...网站可能容易受到跨站脚本攻击?),这就使得解析器认为页面的其余内容都是属性值。 (这将需要另一个"或一个>里面的一个真实的属性值,使其认识到这个错误,我认为)。

不幸的是,这是一个相当棘手的错误修复后。也许你可以尝试一个稍微不同的解析器?例如。如果您使用该版本,请尝试Soup 3.0.x而不是3.1.x,反之亦然。或者尝试html5lib。

+0

@bobince,很好的发现!我在管理html5lib之后很快就放弃了这个烂摊子,看起来 - 没有发现额外双引号的早期错误。 +1为鹰眼! - ) – 2009-11-09 02:20:44

+0

这就是为什么美丽的汤有“标记按摩”功能。您可以提供RE来发现此特定问题并修复损坏的报价。 – 2009-11-09 03:20:21

+0

说实话,我不知道浏览器的解析器是如何处理它的! – bobince 2009-11-09 09:51:25