如何操作URL字符串以提取单个片段?

2024-09-30 22:15:56 发布

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

我不熟悉编程和Python。你知道吗

背景

我的程序接受url。我想从url中提取用户名。你知道吗

用户名是子域。 如果子域是“www”,用户名应该是域的主要部分。域的其余部分应该丢弃(例如“.com/”、“.org/”)

我试过以下方法:

def get_username_from_url(url):
    if url.startswith(r'http://www.'):
        user = url.replace(r'http://www.', '', 1)
        user = user.split('.')[0]
        return user
    elif url.startswith(r'http://'):
        user = url.replace(r'http://', '', 1)
        user = user.split('.')[0]
        return user

easy_url = "http://www.httpwwwweirdusername.com/"    
hard_url = "http://httpwwwweirdusername.blogger.com/"

print get_username_from_url(easy_url)
# output = httpwwwweirdusername (good! expected.)

print get_username_from_url(hard_url)
# output = weirdusername (bad! username should = httpwwwweirdusername)

我用strip()split()replace()尝试了许多其他组合。你知道吗

你能告诉我如何解决这个相对简单的问题吗?你知道吗


Tags: 子域fromcomhttpurlgetreturnwww
2条回答

有一个名为^{}的模块专门用于此任务:

>>> from urlparse import urlparse
>>> url = "http://httpwwwweirdusername.blogger.com/"
>>> urlparse(url).hostname.split('.')[0]
'httpwwwweirdusername'

http://www.httpwwwweirdusername.com/的情况下,它将输出不需要的www。有一些解决方法可以忽略www部分,例如,从分割的hostname中获取不等于www的第一个项:

>>> from urlparse import urlparse

>>> url = "http://www.httpwwwweirdusername.com/"
>>> next(item for item in urlparse(url).hostname.split('.') if item != 'www')
'httpwwwweirdusername'

>>> url = "http://httpwwwweirdusername.blogger.com/"
>>> next(item for item in urlparse(url).hostname.split('.') if item != 'www')
'httpwwwweirdusername'

使用正则表达式可以做到这一点(可能会修改regex使其更精确/更高效)。你知道吗

import re
url_pattern = re.compile(r'.*/(?:www.)?(\w+)')
def get_username_from_url(url):
    match = re.match(url_pattern, url)
    if match:
        return match.group(1)

easy_url = "http://www.httpwwwweirdusername.com/"
hard_url = "http://httpwwwweirdusername.blogger.com/"

print get_username_from_url(easy_url)
print get_username_from_url(hard_url)

这就产生了:

httpwwwweirdusername
httpwwwweirdusername

相关问题 更多 >