Extracting multiple Wikipedia pages using Pythons Wikipedia

Viewed 76

I am not sure how to extract multiple pages from a search result using Pythons Wikipedia plugin. Some advice would be appreciated.

My code so far:

import wikipedia as wiki
import pprint as pp
from transformers import BertTokenizer, BertForSequenceClassification

model = BertForSequenceClassification.from_pretrained('bert-base-uncased')
question = 'New Zealand'
tokenizer = BertTokenizer.from_pretrained('bert-base-uncased')
results = wiki.search(question)
print("Wikipedia search results for our question:\n")
pp.pprint(results)

page = wiki.page(results[0])
text = page.content

How can I get page to hold all the different search results?

1 Answers

You have done the hard part, the results are already in the results variable.

But the results need parsing by the wiki.page() nethod, which only takes one argument.

The solution? Use a loop to parse all results one by one.

The easiest way will be using for loops, but the list comprehension method is the best.

Replace the last two lines with the following:

text = [wiki.page(i).content for i in results]

You should get all the texts in the text variable, which is a list.

Related