webから単語をとってくる。

単語と判定するにはどうするのか。。英語だったら、とりあえずスペースで区切ったものを拾ってくればいいか。

>>> from urllib import urlopen
>>> src=urlopen("http://python.org/")
>>> words=src.read().split()
>>> words
['<!DOCTYPE', 'html', 'PUBLIC', '"-//W3C//DTD', 'XHTML', '1.0', 'Transitional//EN"', '"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">', '<html', 'xmlns="http://www.w3.org/1999/xhtml"', 'xml:lang="en"', 'lang="en">', '<head>', '<meta', 'http-equiv="content-type"', 'content="text/html;', 'charset=utf-8"', '/>', '<title>Python', 'Programming', 'Language', '--', 'Official', 'Website</title>', '<meta', 'name="keywords"', 'content="python', 'programming', 'language', 'object', 'oriented', 'web', 'free', 'source"', '/>', '<meta', 'name="description"'
以下、長過ぎるので略。

こんな感じでsplit()メソッドを使ったらスペースで文章を分解して単語リストを作れる。
ただ、欲しくないデータもたくさんある。HTMLタグとか。
こやつらを削除したい。
こういうときに正規表現でマッチさせるってのは使えるな。
'<', '>', '/', ':'とかが入っている単語は削除するっていうのはどうか

>>> import re
>>> match=re.compile("[<>/:]")
>>> cnt=0
>>> for word in words:
...     if match.search(word):
...             del words[cnt]
...     cnt+=1
... 
>>> words
['html', 'PUBLIC', 'XHTML', '1.0', '"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">', 'xmlns="http://www.w3.org/1999/xhtml"', 'lang="en">', '<meta', 'http-equiv="content-type"', 'charset=utf-8"', '<title>Python', 'Programming', 'Language', '--', 'Official', '<meta', 'name="keywords"', 'content="python', 'programming', 'language', 'object', 'oriented', 'web', 'free', 'source"', '<meta', 'name="description"', 'content="', 'Home', 'page', 'for', 'Python,', 'an', 'interpreted,', 'interactive,', 'object-oriented,', 'extensible', 'programming', 'language.', 'It', 'provides', 'an', 'extraordinary', 'combination', 'of', 'clarity',
以下長いので略

ちょっと減ったがまだ<とか>とか入っている単語が残っているのはどういうことか?

>>> for word in words:
...     if match.search(word):
...             print word
... 
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
xmlns="http://www.w3.org/1999/xhtml"
lang="en">
<meta
<title>Python
<meta
<meta
<link
略

いうふうにマッチはちゃんとできているので、
どうもdel words[cnt]で削除できていないときがあるということか。


とりあえず的戦法でこんな風にする。

>>> match=re.compile(r'[<>=:/\-]')
>>> res=[]
>>> res
[]
>>> for word in words:
...     if not match.search(word):
...             res.append(word)
... 
>>> res
['html', 'PUBLIC', 'XHTML', '1.0', 'Programming', 'Language', 'Official', 'programming', 'language', 'object', 'oriented', 'web', 'free', 'source"', 'Home', 'page', 'for', 'Python,', 'an', 'interpreted,', 'interactive,', 'extensible', 'programming', 'language.', 'It', 'provides', 'an', 'extraordinary', 'combination', 'of', 'clarity', 'and', 'versatility,', 'and', 'is', 'free', 'and', 'comprehensively', 'ported."', 'Events"', 'Recipes"', 'News"', 'Screencasts"', 'Podcasts"', 'News"', 'Enhancement', 'Proposals"', 'stylesheet"', 'text"', 'stylesheet"', 'fonts"', 'under', 'the', 'www.python.org', 'Domain"', 'within', 'the', 'Python', 'Wiki"', 'within', 'Python', 'Books', 'at', 'Google', 'Book', 'Search"', 'within', 'the', 'Python', 'Documentation"', 'for', 'a', 'Module', 'in', 'the', 'Standard', 'Library"', 'for', 'Packages', 'inside', 'the', 'Cheeseshop', '(PyPI)"', 'Archives', 
略

だいぶ単語だけにしぼれた。