To achieve this, you can use the Wikipedia API to fetch the content of the first paragraph from a specific Wikipedia page and then save it to a text file. Here’s a general outline of how you can do this in Python on your Raspberry Pi:
-
Install the Wikipedia API library if you haven’t already:
bashpip install wikipedia-api
-
Write a Python script to fetch the first paragraph of a Wikipedia page and save it to a text file:
pythonimport wikipediaapi def fetch_first_paragraph(term, lang='en'): wiki_wiki = wikipediaapi.Wikipedia(lang) page = wiki_wiki.page(term) if page.exists(): return page.summary.split('\n')[0] return None def save_to_file(text, filename): with open(filename, 'w') as file: file.write(text) if __name__ == "__main__": term = input("Enter the search term: ") lang = input("Enter the language (e.g., 'en' for English): ") paragraph = fetch_first_paragraph(term, lang) if paragraph: save_to_file(paragraph, f"{term}_first_paragraph.txt") print("First paragraph saved to file successfully!") else: print("Error: Page not found or no summary available.")
-
Run the script on your Raspberry Pi, providing the search term and the language code when prompted. For example, if you want to search for “Tiger” in Simple English, you would enter “Tiger” as the search term and “simple” as the language code.
This script should fetch the first paragraph of the Wikipedia page for the specified term and save it to a text file named {term}_first_paragraph.txt
in the current directory.
المزيد من المعلومات
لتنفيذ هذا المشروع، يمكنك استخدام لغة Python على Raspberry Pi للوصول إلى API خاصة بويكيبيديا واسترداد المحتوى الخاص بالفقرة الأولى من صفحة ويكيبيديا المطلوبة. يجب أولاً تثبيت مكتبة ويكيبيديا API إذا لم يكن مثبتًا بالفعل:
bashpip install wikipedia-api
ثم، يمكنك كتابة الكود التالي للقيام بالمهمة:
pythonimport wikipediaapi
def fetch_first_paragraph(term, lang='en'):
wiki_wiki = wikipediaapi.Wikipedia(lang)
page = wiki_wiki.page(term)
if page.exists():
return page.summary.split('\n')[0]
return None
def save_to_file(text, filename):
with open(filename, 'w') as file:
file.write(text)
if __name__ == "__main__":
term = input("Enter the search term: ")
lang = input("Enter the language (e.g., 'en' for English): ")
paragraph = fetch_first_paragraph(term, lang)
if paragraph:
save_to_file(paragraph, f"{term}_first_paragraph.txt")
print("First paragraph saved to file successfully!")
else:
print("Error: Page not found or no summary available.")
هذا الكود يسمح لك بإدخال مصطلح البحث ولغة البحث، ثم يقوم بالبحث عن صفحة ويكيبيديا المرتبطة بهذا المصطلح واسترداد الفقرة الأولى من الملخص الخاص بالصفحة وحفظها في ملف نصي.