Search code examples
pythonwikipediawikidata

Python Library to retrieve Wikipedia page summary from URL


From a previous question on the StackOverflow How to get Wikipedia page from Wikidata Id?, The following answer helped me retrieve the Wikipedia URL for a corresponding Wikidata Identifier

   def get_wikipedia_url_from_wikidata_id(wikidata_id, lang='en', debug=False):
    import requests
    from requests import utils

    url = (
        'https://www.wikidata.org/w/api.php'
        '?action=wbgetentities'
        '&props=sitelinks/urls'
        f'&ids={wikidata_id}'
        '&format=json')
    json_response = requests.get(url).json()
    if debug: print(wikidata_id, url, json_response) 

    entities = json_response.get('entities')    
    if entities:
        entity = entities.get(wikidata_id)
        if entity:
            sitelinks = entity.get('sitelinks')
            if sitelinks:
                if lang:
                    # filter only the specified language
                    sitelink = sitelinks.get(f'{lang}wiki')
                    if sitelink:
                        wiki_url = sitelink.get('url')
                        if wiki_url:
                            return requests.utils.unquote(wiki_url)
                else:
                    # return all of the urls
                    wiki_urls = {}
                    for key, sitelink in sitelinks.items():
                        wiki_url = sitelink.get('url')
                        if wiki_url:
                            wiki_urls[key] = requests.utils.unquote(wiki_url)
                    return wiki_urls
    return None 

My question is: Is there a python library that can retrieve the summary from the Wikipedia URL rather than writing a python scraper code using beautiful soap library ?

Example:

res= get_wikipedia_url_from_wikidata_id('Q34211')
res
https://ar.wikipedia.org/wiki/ياسر_عرفات

The summary should be :

ياسر عرفات (وُلد في مدينة القاهرة، المملكة المصرية في 29 صفر 1348 هـ / 4 أغسطس 1929 – تُوفي في باريس، فرنسا في 28 رمضان 1425 هـ / 11 نوفمبر 2004 )، واسمه الحقيقي محمد عبد الرؤوف عرفات القدوة الحسيني. يُكنى بأبي عمار ويُلقب بالختيار. هو سياسي وعسكري فلسطيني لاجئ وأحد مؤسسي حركة فتح وجناحها المسلح (العاصفة).
هو رئيس منظمة التحرير الفلسطينية منذ 1969 وحتى 2004، وثالث شخص يتقلد هذا المنصب منذ تأسيسها عام 1964، وهو القائد العام لحركة فتح أكبر الحركات داخل المنظمة التي أسسها مع رفاقه في عام 1959. عارض منذ البداية الوجود الإسرائيلي ولكنه عاد وقبِل بقرار مجلس الأمن الدولي رقم 242 في أعقاب هزيمة يونيو 1967، وموافقة منظمة التحرير الفلسطينية على قرار حل الدولتين والدخول في مفاوضات سرية مع الحكومة الإسرائيلية. كرس معظم حياته لقيادة النضال الوطني الفلسطيني مطالبًا بحق الشعب الفلسطيني في تقرير مصيره.
شرع عرفات ومنظمة التحرير الفلسطينية في آخر فترات حياته في سلسلة من المفاوضات مع إسرائيل لإنهاء عقود من الصراع الإسرائيلي الفلسطيني. ومن تلك المفاوضات مؤتمر مدريد 1991، واتفاقية أوسلو. ترأس السلطة الوطنية الفلسطينية في عام 1996 بعد انتخابه. أدانت الفصائل الإسلامية وبعض اليسارية والعضوة في منظمة التحرير الفلسطينية التنازلات التي قُدمت للحكومة الإسرائيلية، وأصبحوا من المعارضة. وفي عام 1994 مُنحت جائزة نوبل للسلام لياسر عرفات، وإسحاق رابين، وشمعون بيريز بسبب مفاوضات أوسلو.
بنهاية سنة 2004، مرض ياسر عرفات بعد سنتين من حصار للجيش الإسرائيلي له داخل مقره في رام الله، ودخل في غيبوبة. توفي ياسر عرفات في 11 نوفمبر 2004 في مستشفى بيرسي العسكري بباريس عن عمر جاوز 75 عاما. لا يعرف سبب الوفاة على التحديد، وقد قال الأطباء أن سبب الوفاة هو تليف الكبد، ولكن لم يتم تشريح الجثة.

Solution

  • You can fetch this api and work with the json data

    https://ar.wikipedia.org/api/rest_v1/page/summary/ياسر_عرفات

    You can replace summary with one of this functions to reach the required data

    • data-parsoid
    • html
    • lint
    • media-list
    • mobile-html
    • mobile-html-offline-resources
    • mobile-sections
    • mobile-sections-lead
    • mobile-sections-remaining
    • pdf
    • random
    • related
    • segments
    • summary
    • talk
    • title
    • wikitext