使用ajax从wikipedia api渲染图像

时间:2015-05-01 07:52:02

标签: javascript jquery ajax api wikipedia

我正在尝试渲染维基百科文章的图像。

例如来自https://de.wikipedia.org/wiki/Appendizitis

为避免跨源策略,我使用代理:

user> (clojure.pprint/pprint (extract-items items item-keys))
({"item_name" "Great Deal",
  "item_options" "foo: 3",
  "item_quantity" "1",
  "item_price" "9.99"}
 {"item_options" "blah: 2",
  "item_name" "Awesome Deal",
  "item_price" "9.99"})
nil

我如何只解析图像? 或者,javascript,jquery和ajax有更好的方法吗?我不想使用PHP。

2 个答案:

答案 0 :(得分:2)

function imageWp() {

    var word = 'Appendizitis';

    $.ajaxPrefilter(function (options) {
        if (options.crossDomain && jQuery.support.cors) {
            var https = (window.location.protocol === 'http:' ? 'http:' : 'https:');
            options.url = https + '//cors-anywhere.herokuapp.com/' + options.url;
        }
    });

    $.get(
        'https://de.wikipedia.org/w/api.php?action=parse&format=json&prop=text&section=0&page=' + word + '&callback=?',

    function (response) {
        var m;
        var urls = [];
        var regex = /<img.*?src=\\"(.*?)\\"/gmi;

        while (m = regex.exec(response)) {
            urls.push(m[1]);
        }

        urls.forEach(function (url) {
            $("#viewer").append('<img src="' + window.location.protocol + url + '">');
        });
    });
}

imageWp();
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="viewer"></div>

答案 1 :(得分:0)

查看维基百科API:

http://en.wikipedia.org/wiki/Special%3aApiSandbox#action=query&prop=images&format=json&titles=Appendicitis&uselang=de

您可以查询文章并以JSON格式获取图像列表:

{
    "warnings": {
        "query": {
            "*": "..."
        }
    },
    "query-continue": {
        "images": {
            "imcontinue": "70980|Stomach_colon_rectum_diagram-en.svg"
        }
    },
    "query": {
        "pages": {
            "70980": {
                "pageid": 70980,
                "ns": 0,
                "title": "Appendicitis",
                "images": [
                    {
                        "ns": 6,
                        "title": "File:Acute Appendicitis.jpg"
                    },
                    {
                        "ns": 6,
                        "title": "File:Acute appendicitis High Power.jpg"
                    },
                    {
                        "ns": 6,
                        "title": "File:Appendicitis - low mag.jpg"
                    },
                    {
                        "ns": 6,
                        "title": "File:Appendicitis world map - DALY - WHO2004.svg"
                    },
                    {
                        "ns": 6,
                        "title": "File:Appendix-Entfernung.jpg"
                    },
                    {
                        "ns": 6,
                        "title": "File:CAT scan demonstrating acute appendicitis.jpg"
                    },
                    {
                        "ns": 6,
                        "title": "File:Commons-logo.svg"
                    },
                    {
                        "ns": 6,
                        "title": "File:Inflamed appendix.jpg"
                    },
                    {
                        "ns": 6,
                        "title": "File:SonoAppendizitis.JPG"
                    },
                    {
                        "ns": 6,
                        "title": "File:Stitches post appendicitis surgery.jpg"
                    }
                ]
            }
        }
    }
}