如何修复 Python 3 AttributeError: 'function' object has no attribute 'urlsplit'

问题:

尝试在 Python 3 中对 URL 进行 urlsplit 时:

urlsplit_misuse_example.py
from urllib.parse import urlparse

path = urlparse.urlsplit(remote_image_url).path
urlsplit_misuse_example.py
from urllib.parse import urlparse

path = urlparse.urlsplit(remote_image_url).path

你看到以下错误消息:

urlsplit_attributeerror.txt
AttributeError                            Traceback (most recent call last)
Input In [30], in <cell line: 3>()
      1 from urllib.parse import urlparse
----> 3 path = urlparse.urlsplit(remote_image_url).path
      4 filename = posixpath.basename(path)

AttributeError: 'function' object has no attribute 'urlsplit'
traceback.txt
AttributeError                            Traceback (most recent call last)
Input In [30], in <cell line: 3>()
    1 from urllib.parse import urlparse
----> 3 path = urlparse.urlsplit(remote_image_url).path
    4 filename = posixpath.basename(path)

AttributeError: 'function' object has no attribute 'urlsplit'

解决方案

Python 3 中 urlparse.urlsplit() 的等价物是 urllib.parse.urlsplit()

因此,一个可用的代码示例是

urlsplit_fix_example.py
from urllib.parse import urlsplit

path = urlsplit(remote_image_url).path
urlsplit_fix_example.py
from urllib.parse import urlsplit

path = urlsplit(remote_image_url).path

Check out similar posts by category: Python