【发布时间】:2014-09-08 10:13:44
【问题描述】:
我收到以下错误:
Traceback (most recent call last):
File "2.py", line 22, in <module>
i.string.replace_with(i.string.replace(u'\xa0', '-'))
AttributeError: 'NoneType' object has no attribute 'replace_with'
部分代码
soup = bs4(open("test.html"))
table = soup.find("table", {"color":"#fff"})
for i in soup.find_all('small'):
i.string.replace_with(i.string.replace(u'\xa0', '-')) <--Line 22
昨天它还在工作,但我不得不在另一个 VM 上重新安装 Mint,但我无法让它再次工作。我该如何解决?
编辑:这是所有代码:
from bs4 import BeautifulSoup as bs4
soup = bs4(open("test.html"))
table = soup.find("table", {"color":"#fff"})
for i in soup.find_all('small'):
i.string.replace_with(i.string.replace(u'\xa0', '-'))
#print soup
f = open("new.html", "w")
f.write(str(table))
这是 test.html 中的表格:
<table color="#fff">
<tr>
<td><small><small> </small></small></td>
</tr>
</table>
【问题讨论】:
-
发布更多代码。
i是什么? -
@stalk 添加了一些代码。我正在阅读一个 html 文件
-
是
replace_with()还是replace_width()?你在你的问题中都使用了。会不会是您的代码中的拼写错误? -
@FrédéricHamidi 抱歉,我打错了。它是 replace_with
-
显然
i.string是None。为什么你认为i.string不应该是None?你明白错误信息了吗?
标签: python python-2.7