Python 维基百科 API。计数功能

Python Wikipedia API. Count function

我正在使用 Python 维基百科库 API 来解析来自维基百科的数据。我在计数函数上遇到 TypeError。

代码如下:

 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'

lexus_count() 接受参数 vehicles 而你忘记将它传递给第 32 行的函数