import requests
from bs4 import BeautifulSoup


def get_lcs(array):
    """
    Get the Least common subsumers between several Wikidata qids in a list
    Return a Python list
    """
    array_string = ", ".join(array)
    query = {"query": """
    SELECT ?lcs ?lcsLabel WHERE {
    ?lcs ^wdt:P279* %s .
    filter not exists {
    ?sublcs ^wdt:P279* %s ;
          wdt:P279 ?lcs .
      }
    SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en " . } }""" % (array_string, array_string)
             }
    url = "https://query.wikidata.org/sparql"
    r = requests.get(url, params=query)
    soup = BeautifulSoup(r.text, "lxml")

    return [x.text for x in soup.find_all("literal")]