我在使用 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