2011-09-19 11 views
12

私は非常にPythonに慣れています。非常に新しい。NameError:名前 're'が定義されていません

Traceback (most recent call last): 
    File "test.py", line 8, in <module> 
    patFinderTitle = re.compile('<title>(.*)</title>') 
NameError: name 're' is not defined 

は私が間違って何をやっている:私は、私はエラーを取得するチュートリアルから

#!/usr/bin/python 
import re 
from urllib import urlopen 
from BeautifulSoup import BeautifulSoup 

webpage = urlopen('http://feeds.huffingtonpost.com/huffingtonpost/LatestNews').read 

patFinderTitle = re.compile('<title>(.*)</title>') 

patFinderLink = re.compile('<link rel.*href="(.*)"/>') 

findPatTitle = re.findall(patFinderTitle,webpage) 

findPatLink = re.findall(patFinderLink,webpage) 

listIterator = [] 
listIterator[:] = range(2,16) 

for i in listIterator: 
    print findPatTitle[i] 
    print findPatLink[i] 
    print "\n" 

を以下にコピーしましたか?

編集:私はimport reを追加しましたが、現在は次を得る:

File "/scripts/_prod/test.py", line 13, in <module> 
    findPatTitle = re.findall(patFinderTitle,webpage) 
    File "/usr/lib64/python2.6/re.py", line 177, in findall 
    return _compile(pattern, flags).findall(string) 
TypeError: expected string or buffer 
+0

このチュートリアルをコピーしましたか?それはエラーで散在しています。 – Johnsyweb

+0

http://www.youtube.com/watch?v=Ap_DlSrT-iE&feature=related –

+0

次にコードを以下のコードと比較してください:http://www.newthinktank.com/2010/11/python-2 -7-tutorial-pt-13-website-scraping /。ちょっと整理した後、私はそれが動作することがわかった。ありがとう。 – Johnsyweb

答えて

19

あなたはあなたのコード内でregular expression module

import re 
re.compile('<title>(.*)</title>') 
+0

ありがとう。私は今、別のエラーが発生しました。私の編集を見てください –

+3

@ user522962 'webpage = urlopen( 'http://feeds.huffingtonpost.com/huffingtonpost/LatestNews').read'は' webpage = urlopen( 'http:// feeds.huffingtonpost.com/huffingtonpost/LatestNews ')。read() ' – razpeitia

1

をインポートする必要が現在webpageは、関数への参照です。 ()をオフにしたことが疑われます。read

関連する問題