Python 维基百科,免费的百科全书

Python Wikipedia library

我正在使用 python 库维基百科来解析数据。当它到达代码的第二部分时,我遇到了页面错误。Page Errors

import wikipedia


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)':")
print (result)
print

lexus_count = result.count('ct','lfa','rx')
print


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

已更新 我能够解析页面数据,但在计数时出现类型错误

23 print
24
25 lexus_count = result.count('ct','lfa','rx')
26 print
TypError: slice indices must be integers or None or have an __index__ method

您到底遇到了哪个页面错误?

根据维基百科文档:https://wikipedia.readthedocs.io/en/latest/quickstart.html#quickstart

But watch out - wikipedia.summary will raise a DisambiguationError if the page is a disambiguation page, or a PageError if the page doesn’t exist (although by default, it tries to find the page you meant with suggest and search.):

您的程序存在多个问题。这是一个更新的程序,修复并标记了错误。

import wikipedia


print ("1: Searching Wikipedia for 'Lexus'")
try:
    print (wikipedia.page('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 'Lexus (company)'")
result = wikipedia.page('Lexus (company)') 
# ERR; PAGE NAME SEPARATED BY SPACE NOT WITH AN UNDERSCORE
# <> PAGE ERROR AS PAGE WILL NOT BE FOUND.  
print (result)
print


result = result.content
print ("3: Result of searching Wikipedia for 'Lexus_(company)':")
print (result)
print

lexus_count = result.count('Lexus')
# changed variable name from orange_count -> lexus_count, as referenced in the print function below. 
# you were counting for 'lexus' you will not find any occurrences as this function is case sensitive.
print


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

希望这对您有所帮助。