2017-02-22 51 views
0

我使用python庫維基百科來解析數據。當它到達代碼的第二部分時,我會收到頁面錯誤。 Page ErrorsPython Wikipedia庫

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 
+1

請將您收到的錯誤複製並粘貼到問題中,而不是發佈文字圖像。 – jwodder

+1

當我在維基百科上搜索雷克薩斯(公司)時,結果顯示:'雷克薩斯(公司)「頁面不存在。您可以要求創建它,但請考慮檢查下面的搜索結果,以確定該主題是否已被覆蓋。所以錯誤信息是有道理的 – smoggers

回答

0

您的程序有多個問題。這是一個更新的程序,錯誤已修復並標記出來。

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 

希望這會有所幫助。

0

你到底是哪一個得到錯誤頁的頁面數據,但得到錯誤類型?

根據維基百科的資料:https://wikipedia.readthedocs.io/en/latest/quickstart.html#quickstart

但要小心 - wikipedia.summary將引發DisambiguationError如果頁面是一個消歧義頁,或PageError如果頁面不存在(雖然默認,它會嘗試找到您提供的建議和搜索頁面。):