urlparse类型/属性错误

2024-10-02 00:22:45 发布

您现在位置:Python中文网/ 问答频道 /正文

Python2.7。在

我确定这是一个愚蠢的问题,但我真的被urlspilt中的类型/属性错误弄糊涂了。在

我试图遍历unicode url的列表,拆分并重新构建它们以删除部分url。在

new_list = []
for line in list_of_links:
    urlsplit(line)
    new_list.append(line.scheme + '://' + line.netloc + line.path)

返回AttributeError: 'unicode' object has no attribute 'scheme'

将每行编码为utf-8不起作用。str(line).scheme返回'str' object has no attribute 'scheme'

我错过了什么?在


Tags: nourl类型new属性object错误line
1条回答
网友
1楼 · 发布于 2024-10-02 00:22:45

line仍然是str,而字符串没有这些方法。urlparse()urlsplit()分别返回ParseResult和{}命名的元组对象。参见文档here。只有它们具有schemenetloc属性。要正确使用它们,请将结果存储在变量中,如下所示:

new_list = []
for line in list_of_links:
    urlsplit_result = urlsplit(line)
    new_list.append(urlsplit_result.scheme + '://' + urlsplit_result.netloc + urlsplit_result.path)

相关问题 更多 >

    热门问题