关于urlopen的简单python问题

Jae*_*bum 1 python

我正在尝试制作一个删除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, "")
Run Code Online (Sandbox Code Playgroud)

但它显示如下错误消息

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'
Run Code Online (Sandbox Code Playgroud)

有一点很有意思的是,如果我将代码输入到python中,上面的错误就不会显示出来.

Ign*_*ams 5

你已经命名了一个脚本string.py.该urllib模块导入这个,认为它是相同的string模块,这是在STDLIB,然后你的代码使用属性现在部分定义上urllib是不存在的模块.将脚本命名为其他内容.