2011-09-19 10 views
12

मैं अजगर के लिए बहुत नया हूं। बहुत नया। मैं एक ट्यूटोरियलनाम त्रुटि: नाम 'पुनः' परिभाषित नहीं किया गया है

#!/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" 

से निम्नलिखित की नकल की मैं त्रुटि मिलती है:

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

क्या मैं गलत कर रहा हूँ?

संपादित करें: मैं 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-ट्यूटोरियल-pt-13-वेबसाइट-स्क्रैपिंग /। थोड़ा साफ-सुथरा होने के बाद मैंने पाया कि यह काम करता है। – 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 ')। पढ़ें() ' – razpeitia

1

वर्तमान में webpage एक समारोह के लिए एक संदर्भ है की जरूरत है। मुझे संदेह है कि read