无法使用 python urlparse 解析 Url

Unable to parse Url with python urlparse

我正在尝试编写一个将 url 作为输入并对其进行解析的小脚本。

以下是我的脚本

#! /usr/bin/env python

import sys

from urlparse import urlsplit
url = sys.argv[1]
parseUrl = urlsplit(url)
print 'scheme  :', parseUrl.scheme
print 'netloc  :', parseUrl.netloc

但是当我用 ./myscript http://www.example.com

执行这个脚本时

显示以下错误。

AttributeError: 'tuple' object has no attribute 'scheme'

我是python/scripting的新手,我哪里做错了?

编辑: Python 我用的版本是Python 2.7.5

你不想要计划。相反,在这种情况下,您想要访问元组的 0 索引和元组的 1 索引。

print 'scheme  :', parseUrl[0]
print 'netloc  :', parseUrl[1]

urlparse 使用 .scheme.netloc 表示法,urlsplit instead uses a tuple (refer to the appropriate index number):

This is similar to urlparse(), but does not split the params from the URL. This should generally be used instead of urlparse() if the more recent URL syntax allowing parameters to be applied to each segment of the path portion of the URL (see RFC 2396) is wanted. A separate function is needed to separate the path segments and parameters. This function returns a 5-tuple: (addressing scheme, network location, path, query, fragment identifier).

The return value is actually an instance of a subclass of tuple. This class has the following additional read-only convenience attributes:

Attribute Index   Value                               Value if not present
scheme      0       URL scheme specifier                empty string
netloc      1       Network location part               empty string
path        2       Hierarchical path                   empty string
query       3       Query component                     empty string
fragment    4       Fragment identifier                 empty string
username            User name                           None
password            Password                            None
hostname            Host name (lower case)              None
port                Port number as integer, if present  None

查看文档,听起来您使用的是 Python 2.4,它没有添加属性。另一个回答漏掉了文档中的关键部分:

New in version 2.2.

Changed in version 2.5: Added attributes to return value.

您将必须通过索引或解包访问元组部分:

scheme, netloc, path, query, fragment = urlsplit(url)

但是,您真的应该升级到 Python 2.7。 Python 2.4 不再受支持。