Python 3.10.2
我有一個通常如下所示的 URL,有一些細微的變化(有時是 http/https、www. 前綴,末尾的 #params 表示諸如參考者或正在顯示的設備等內容)。
https://madeupdomain.net/u/Hypothetical_Username/Some-Random-Page-Name
我通常遇到的 URL 的形式是:
https://madeupdomain.net/u/Hypothetical_Username/
要么
https://madeupdomain.net/u/Hypothetical_Username/Some-Random-Page-Name
我對使用 URL 感興趣的內容:
- 獲取
Hypothetical_Username零件 - 查明 URL 是否在用戶名處停止,或者
/path后面是否有另一個
我一直在使用user = url.split('/')[4]獲取 URL 的用戶名部分。由于 URL 總是包含用戶名并且 URL 通常是一致的(目前),我可以依靠這個拆分來獲取我想要的元素。如果 URL 將來發生一點變化,我知道這會讓我感到厭煩。
但是,路徑的其余部分是可選的。
如果我只使用url.split('/')[5],python 會在遇到 split 沒有[5]th 元素的 URL 時立即拋出錯誤。
所以我厭倦了用 if 陳述句“測驗”它,它仍然抱怨并拋出錯誤IndexError: list index out of range。
if url.split('/')[5]:
continue
當我列印出串列時,它會如下所示。如您所見,第一個元素有 5 個,第二個元素有 6 個。
['https:', '', 'madeupdomain.net', 'u', 'Hypothetical_Username']
['https:', '', 'madeupdomain.net', 'u', 'Hypothetical_Username', 'Some-Random-Page-Name']
所以,我嘗試len(url.split('/'))在每次迭代中運行,看看每個串列有多少元素,它總是說 6 - 無論是上面的第一個還是第二個例子。
So, I'm kind of at a loss here as to a very simple and clean way to do what I want to do. I know there are url parsing libraries, but that seems like overkill for what I want to do (get the username, then find out if there is a path name beyond that and decide what to do with the URL once I know).
Would really appreciate any guidance here. I know I'm just bashing my head against something really simple.
Thanks for your input.
Solutions Both @Desktop-Firework and @Kaushal-Sharma's solutions worked well, in different ways. I also wanted to add the simplest way to do what I was originally trying to do once I got it to work based on their answers. It's obvious to anyone above my level of experience with Python, but maybe it'll help someone in my situation down the line.
我只是在做一個“if”來檢查索引點是否存在,而我顯然應該使用 try-except。
因此,使用我的原始代碼,我可以通過簡單地更改來解決我需要的問題:
if url.split('/')[5]:
continue
進入
isPath = 1
try: link.split("/")[5]
except IndexError: isPath = 0
只需添加它,因為它直接回答了我在其最基本元素上嘗試做的事情。顯然,它不像其他貢獻者提供的解決方案那樣健壯或優雅。
uj5u.com熱心網友回復:
我建議獲取 的索引,然后將 之后和之前的/u/每個字符作為用戶名的一部分,然后嘗試獲取用戶名之后的字符。如果有,則用戶名后面沒有路徑;如果沒有,那就有路徑。/u///IndexError
所以我提出這樣的建議:
def getUserName(url):
userStart = url.index('/u/') 3
urlIdx = userStart
userName = ''
while url[urlIdx] != '/':
userName = url[urlIdx]
urlIdx = 1
urlIdx = 1
isPath = 1
try: url[urlIdx]
except IndexError: isPath = 0
return (userName, isPath)
它回傳一個元組,其中第一個元素是用戶名,第二個元素是用戶名之后是否有路徑。但在這種情況下,https://www.example.net/u/username/它只有/在用戶名后面有一個時才有效。
uj5u.com熱心網友回復:
您可以在“/u/”處拆分網址,然后用“/”拆分最后一部分以獲取用戶名和之后的路徑。
# case 1:
url = 'https://madeupdomain.net/u/Hypothetical_Username/Some-Random-Page-Name'
split_url = url.split('/u/')[-1].split('/')
hyp_username_part = split_url[0]
another_path_part = split_url[-1] if len(split_url) == 2 else None
print('username part: ', hyp_username_part, 'path part: ', another_path_part)
# case 2:
url = 'https://madeupdomain.net/u/Hypothetical_Username'
split_url = url.split('/u/')[-1].split('/')
hyp_username_part = split_url[0]
another_path_part = split_url[-1] if len(split_url) == 2 else None
print('username part: ', hyp_username_part, 'path part: ', another_path_part)
輸出:
username part: Hypothetical_Username path part: Some-Random-Page-Name
username part: Hypothetical_Username path part: None
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/435948.html
