Searching Wikipedia using API

牧云@^-^@ 提交于 2019-12-02 16:40:29
octosquidopus

I don't think you can do both in one query.

1. To get the first result, use the Opensearch API.

https://en.wikipedia.org/w/api.php?action=opensearch&search=zyz&limit=1&namespace=0&format=jsonfm

https://en.wikipedia.org/w/api.php
?action=opensearch
&search=zyz          # search query
&limit=1             # return only the first result
&namespace=0         # search only articles, ignoring Talk, Mediawiki, etc.
&format=json         # jsonfm prints the JSON in HTML for debugging.

This will return:

[
    "Zyz",
    [
        "Zyzomys"
    ],
    [
        ""
    ],
    [
        "https://en.wikipedia.org/wiki/Zyzomys"
    ]
]

2. You now have the article name of the first search result. To get the article's first paragram (or description, as you call it), see my answer here: https://stackoverflow.com/a/19781754/908703

actually the wikipedia json api works with a right query only, so I recommend to use the wikipedia search and crawl the actual article and parse it with BeautifulSoup

https://en.wikipedia.org/w/index.php?search=QUERY&title=Special:Search&fulltext=Search

and also there is module call wikipedia does this for you

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!