我正在使用pip install wikipedia
做一个简单的"Philosophy step counter",但无法获得我想要的结果。无论我输入代码的哪一页,都永远找不到与文章前几句话中的任何单词匹配的链接。代码如下:
import os
import string
import wikipedia
wikipedia.set_lang("en")
print("Please type the name of a wikipedia article: ")
page = input()
print("Searching wikipedia for: " + page)
wikiPage = wikipedia.page(page)
print("Using top result: " + wikiPage.title)
# currentPage = wikipedia.page(wikiPage.links[0])
# List of links (sorted alphabetically, makes our job much harder)
links = wikiPage.links
# Split the beginning of the article into words
words = wikipedia.summary(wikiPage, sentences=3).split()
words = [''.join(c for c in s if c not in string.punctuation)
for s in words] # Sanitize list of words to remove punctuation
# comparisons = [a == b for (a, b) in itertools.product(words, links)]
x = 0
while words[x] not in links:
print(words[x])
x = x + 1
newPage = wikipedia.page(words[x])
这是我正在使用的库还是我的代码的错误?如果有任何区别,链接列表似乎按字母顺序排列(因此,为什么我首先要做所有这些操作)