Retrieving image license and author information in wiki commons

后端 未结 6 976
情深已故
情深已故 2021-02-19 07:04

I am trying to use the wikimedia API for wiki commons at:

http://commons.wikimedia.org/w/api.php

It seems like the commons API is very immature

6条回答
  •  盖世英雄少女心
    2021-02-19 07:31

    have a look at Mediawiki and try this function:

    import json, requests
    def extract_image_license(image_name):
    
        start_of_end_point_str = 'https://commons.wikimedia.org' \
                             '/w/api.php?action=query&titles=File:'
        end_of_end_point_str = '&prop=imageinfo&iiprop=user' \
                           '|userid|canonicaltitle|url|extmetadata&format=json'
        result = requests.get(start_of_end_point_str + image_name+end_of_end_point_str)
        result = result.json()
        page_id = next(iter(result['query']['pages']))
        image_info = result['query']['pages'][page_id]['imageinfo']
    
        return image_info
    

    then you call the function and pass in the image name you want to query for example:

    extract_image_license('Albert_Einstein_Head.jpg')
    

提交回复
热议问题