我正在尝试制作一个删除 html 文档中所有标签的程序。所以我做了一个这样的程序。
import urllib
loc_left = 0
while loc_left != -1 :
html_code = urllib.urlopen("http://www.python.org/").read()
loc_left = html_code.find('<')
loc_right = html_code.find('>')
str_in_braket = html_code[loc_left, loc_right + 1]
html_code.replace(str_in_braket, "")
但是它显示如下错误信息
lee@Lee-Computer:~/pyt$ python html_braket.py
Traceback (most recent call last):
File "html_braket.py", line 1, in <module>
import urllib
File "/usr/lib/python2.6/urllib.py", line 25, in <module>
import string
File "/home/lee/pyt/string.py", line 4, in <module>
html_code = urllib.urlopen("http://www.python.org/").read()
AttributeError: 'module' object has no attribute 'urlopen'
有趣的是,如果我将代码输入 python,上面的错误就不会出现了。
请您参考如下方法:
您已将脚本命名为 string.py。 urllib 模块导入它,认为它与 stdlib 中的 string 模块相同,然后您的代码使用现在部分定义的 urllib< 上的属性 尚不存在的模块。为您的脚本命名。
