gpt4 book ai didi

python - 关于urlopen的简单python问题

转载 作者:太空宇宙 更新时间:2023-11-04 08:21:07 24 4
gpt4 key购买 nike

我正在尝试制作一个删除 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.pyurllib 模块导入它,认为它与 stdlib 中的 string 模块相同,然后您的代码使用现在部分定义的 urllib< 上的属性 尚不存在的模块。为您的脚本命名。

关于python - 关于urlopen的简单python问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5053504/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com