使用BeautifulSoup时出现一个奇怪的错误。

这是我正在运行的代码的片段:

while True:
    listing_soup = soupify(urlget(page_url))
    for i in listing_soup.findAll('div', 'searchResultContent'):
       # do some stuff ...

这是抛出的异常:
Traceback (most recent call last):
  File "C:\path\to\script.py", line 71
6, in <module>
    for i in listing_soup.findAll('div', 'searchResultContent'):
  File "c:\python27\BeautifulSoup.py", line 612, in findAll
    return self._findAll(name, attrs, text, limit, generator, **kwargs)
  File "c:\python27\BeautifulSoup.py", line 275, in _findAll
    strainer = SoupStrainer(name, attrs, text, **kwargs)
  File "c:\python27\BeautifulSoup.py", line 660, in __init__
    self.attrs=attrs.copy()
AttributeError: 'str' object has no attribute 'copy'

我在Windows XP Professional上运行Python 2.7.3。该脚本在Ubuntu Linux上效果很好。

注意:

我期望来自网络的数据为UTF,因此python脚本以以下行开头:
# coding=utf-8

最佳答案

从行号来看,您使用的是Beautiful Soup 3.0.0,它没有您尝试使用的“按CSS类搜索”快捷方式(此功能已在3.0.1中重新引入)。更重要的是,您所使用的软件版本已有五年历史了。我建议所有新项目都使用Beautiful Soup 4

很可能您在Ubuntu上看不到该问题,因为您的Ubuntu安装正在运行更新版本的Beautiful Soup。

10-07 13:18
查看更多