How to get the main image from the MediaWiki API?

Hello, I use Curl to get information from Wikipedia, and I want to receive only information about the main image, I do not want to receive all the images of the article. For example .. If I want to get information about all images in English ( http://en.wikipedia.org/wiki/English_language ), I have to go to this address: http://en.wikipedia.org/w/api. php? action = query & titles = English_Language & prop = images but I get the flags of countries in which people speak English in XML:

<?xml version="1.0"?> <api>   <query>
    <normalized>
      <n from="English_language" to="English language" />
    </normalized>
    <pages>
      <page pageid="8569916" ns="0" title="English language">
        <images>
          <im ns="6" title="File:Anglospeak(800px)Countries.png" />
          <im ns="6" title="File:Anglospeak.svg" />
          <im ns="6" title="File:Circle frame.svg" />
          <im ns="6" title="File:Commons-logo.svg" />
          <im ns="6" title="File:Flag of Argentina.svg" />
          <im ns="6" title="File:Flag of Aruba.svg" />
          <im ns="6" title="File:Flag of Australia.svg" />
          <im ns="6" title="File:Flag of Bolivia.svg" />
          <im ns="6" title="File:Flag of Brazil.svg" />
          <im ns="6" title="File:Flag of Canada.svg" />

I only need information about the main image.

+5
source share
4 answers

, " ", , , . :

  • .
  • , , . 60 & times; 60 .
  • , , .

HTML- action=parse HTML img , :

http://en.wikipedia.org/w/api.php?action=parse&page=English_language&prop=text|images

(, , , API, , MediaWiki.)


wikitext , prop=revisions rvprop=content:

http://en.wikipedia.org/w/api.php?action=query&titles=English_language&prop=revisions|images&rvprop=content

, .. , [[Image:...]] . , , , , , prop=images ( , ) ( Image:/File:) wikitext.

, MediaWiki ( ): , , - . , PHP, , wikitext:

foreach ($names as &$name) {
    $name = trim( preg_replace( '/[_\s]+/u', ' ', $name ) );
    $name = preg_quote( $name, '/' );
    $name = preg_replace( '/^(\\\\?.)/us', '(?i:$1)', $name );
    $name = preg_replace( '/\\\\? /u', '[_\s]+', $name );
}
$regexp = '/' . implode( '|', $names ) . '/u';

, :

Anglospeak(800px)Countries.png
Anglospeak.svg
Circle frame.svg
Commons-logo.svg
Flag of Argentina.svg
Flag of Aruba.svg

:

/(?i:A)nglospeak\(800px\)Countries\.png|(?i:A)nglospeak\.svg|(?i:C)ircle[_\s]+frame\.svg|(?i:C)ommons\-logo\.svg|(?i:F)lag[_\s]+of[_\s]+Argentina\.svg|(?i:F)lag[_\s]+of[_\s]+Aruba\.svg/u
+1

! ( 2014 )
PageImages , -Wikimedia.

prop=images prop=pageimages, pageimage <thumbnail> node <page>.

, , ( ) , .


, OpenSearch API <image> XML-, API Query API.

+6

This is how I started to work ...

$.getJSON("http://en.wikipedia.org/w/api.php?action=query&format=json&callback=?", {
    titles: "India",
    prop: "pageimages",
    pithumbsize: 150
  },
  function(data) {
    var source = "";
    var imageUrl = GetAttributeValue(data.query.pages);
    if (imageUrl == "") {
      $("#wiki").append("<div>No image found</div>");
    } else {
      var img = "<img src=\"" + imageUrl + "\">"
      $("#wiki").append(img);
    }
  }
);

 function GetAttributeValue(data) {
  var urli = "";
  for (var key in data) {
    if (data[key].thumbnail != undefined) {
      if (data[key].thumbnail.source != undefined) {
        urli = data[key].thumbnail.source;
        break;
      }
    }
  }
  return urli;
}



<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<html>

<head></head>

<body>
  <div id="wiki"></div>
</body>

</html>
+3
source

You can limit your query to the first image in the article using the parameter imlimit:

http://en.wikipedia.org/w/api.php?action=query&titles=English_Language&redirects&prop=images&imlimit=1

0
source

All Articles