2017-01-30 10 views
1

Pythonで文字列(URL)からさまざまな単語を取得するにはどうすればよいですか?Pythonを使用して文字列URLを単語に分割する

http://www.sample.com/level1/level2/index.html?id=1234 

私のような言葉を取得したい:

http, www, sample, com, level1, level2, index, html, id, 1234 

のpythonを使用して任意のソリューションをのようなURLから 。

ありがとうございました。

+0

としてそれを使用することができて、私はリストに結果を格納します。 –

答えて

4

これは、あなたがすべてのためにそれを行うことができます方法ですURL

import re 
def getWordsFromURL(url): 
    return re.compile(r'[\:/?=\-&]+',re.UNICODE).split(url) 

は今、あなたは

url = "http://www.sample.com/level1/level2/index.html?id=1234" 
words = getWordsFromURL(url) 
+0

ありがとう...それは働いた。 –

+1

あなたが '' http '、' www '、' sample '、' com '、' level1 '、' level2 '、' index '、' html '、' id '、' 1234 '] ' '[' http '、' www.sample.com '、' level1 '、' level2 '、' index.html?id '、' 1234 ']' –

+0

@ Jean-FrançoisFabrere.UNICODEとしてコンパイルしました。 http://stackoverflow.com/questions/41935748/splitting-a-stri ng-url-into-words-us ing-python –

1

だけで正規表現スプリット非alphanumsの最大の順序に従って:

import re 
l = re.split(r"\W+","http://www.sample.com/level1/level2/index.html?id=1234") 
print(l) 

利回り:

['http', 'www', 'sample', 'com', 'level1', 'level2', 'index', 'html', 'id', '1234'] 

これは単純ですが、誰かが指摘したよう_がある場合、機能しません。 、-、...のURL名です。だから、あまり楽しいソリューションは、パス部分を分離することができ、すべての可能なトークンの一覧を表示するために、次のようになります。

l = re.split(r"[/:\.?=&]+","http://stackoverflow.com/questions/41935748/splitting-a-stri‌​ng-url-into-words-us‌​ing-python") 

(私はいくつかの分離記号を忘れている可能性があることを認める)

+1

'http:// stackoverflow.com/questions/41935748/split-a-string-url-into-words-using-python'のようなURLでは機能しません – Himal

+0

@Himalは私の答えをチェックします、それは –

+0

ではありません'[' http '、' www.sample.com '、' level1 '、' level2 '、' index.html?id '、' 1234 ']' –

関連する問題