2017-02-24 114 views
0

我正在使用Python庫維基百科API來解析來自維基百科的數據。計數函數中出現TypeError。Python維基百科API。計數函數

下面是代碼:

import wikipedia 

'Searching Wikipedia for List of Lexus vehicle 

print ("1: Searching Wikipedia for 'List of Lexus vehicles'") 
try: 
    print (wikipedia.page('List of Lexus')) 
    print ('-' * 60) 
except wikipedia.exceptions.DisambiguationError as e: 
    print (str(e)) 
    print ('+' * 60) 
    print ('DisambiguationError: The page name is ambiguous') 
print 

搜索雷克薩斯車:

print ("2: Searching Wikipedia for 'List of Lexus (vehicles)'") 
print (wikipedia.page('List of Lexus_(vehicles)')) 
print 

打印結果:

result = wikipedia.page('List of Lexus_(vehicles)').content.encode('UTF8') 
    print ("3: Result of searching Wikipedia for 'List of Lexus vehicles_(vehicles)':") 
    print (result) 
    print 

計數功能:

def lexus_count(vehicles): 
     lexus_count = result.count(vehicles) 
     print 


    print ("The Wikipedia page for 'List of Lexus_(vehicles)' has " + \ 
     "{} occurrences of the word 'Lexus'".format(lexus_count())) 
    print 

這裏是類型錯誤消息:

--------------------------------------------------------------------------- 
TypeError         Traceback (most recent call last) 
D:\College\Python\Labs\lab 3\kaminski_lab3_p2.py in <module>() 
    30 
    31 print ("The Wikipedia page for 'List of Lexus_(vehicles)' has " + \ 
---> 32  "{} occurrences of the word 'Lexus'".format(lexus_count())) 
    33 print 

TypeError: lexus_count() missing 1 required positional argument: 'vehicles' 

現在我有新的問題NameError:

--------------------------------------------------------------------------- 
NameError         Traceback (most recent call last) 
D:\College\Python\Labs\lab 3\kaminski_lab3_p2.py in <module>() 
    30 
    31 print ("The Wikipedia page for 'List of Lexus_(vehicles)' has " + \ 
---> 32  "{} occurrences of the word 'Lexus'".format(lexus_count(vehicles))) 
    33 print 

NameError: name 'vehicles' is not defined 

更新輸出的不可讀輸出部分:

The Wikipedia page for 'List of Lexus_(vehicles)' has <function lexus_count at 0x000002431D2B28C8> occurrences of the word 'Lexus' 

回答

1

lexus_count()需要參數車輛,並忘記將其傳遞給第32行的功能

+0

好的。謝謝你,我有新問題: – Michael

+0

新錯誤。請查閱我發佈的新代碼片段。 – Michael

+0

我剛剛想出功能lexus_count是越野車 更改爲: 高清lexus_count(車): 回報result.count(車輛) –