Does the Wikipedia API support searches for a specific template?

五迷三道 提交于 2019-11-29 12:17:02

问题


Is it possible to query the Wikipedia API for articles that contain a specific template? The documentation does not describe any action that would filter search results to pages that contain a template. Specifically, I am after pages that contain Template:Persondata. After that, I am hoping to be able to retrieve just that specific template in order to populate genealogy data for the openancestry.org project.

The query below shows that the Albert Einstein page contains the Persondata Template, but it doesn't return the contents of the template, and I don't know how to get a list of page titles that contain the template. http://en.wikipedia.org/w/api.php?action=query&prop=templates&titles=Albert%20Einstein&tlcontinue=736|10|ParmPart

Returns:

<api>
 <query>
  <pages>
   <page pageid="736" ns="0" title="Albert Einstein">
    <templates>
     ...
     <tl ns="10" title="Template:Persondata"/>
     ...
    </templates>
   </page>
  </pages>
 </query>
 <query-continue>
  <templates tlcontinue="736|10|Reflist"/>
 </query-continue>
</api>

I suspect that I can't get what I need from the API, but I'm hoping I'm wrong and that someone has already blazed a trail down this path.


回答1:


You can use the embeddedin query to find all pages that include the template:

curl 'http://en.wikipedia.org/w/api.php?action=query&list=embeddedin&eititle=Template:Persondata&eilimit=5&format=xml'

Which gets you:

<?xml version="1.0"?>
<api>
  <query>
    <embeddedin>
      <ei pageid="307" ns="0" title="Abraham Lincoln" />
      <ei pageid="308" ns="0" title="Aristotle" />
      <ei pageid="339" ns="0" title="Ayn Rand" />
      <ei pageid="340" ns="0" title="Alain Connes" />
      <ei pageid="344" ns="0" title="Allan Dwan" />
    </embeddedin>
  </query>
  <query-continue>
    <embeddedin eicontinue="10|Persondata|595" />
  </query-continue>
</api>

See full docs at mediawiki.org.

Edit Use embeddedin query instead of backlinks (which doesn't cover template inclusions)




回答2:


Using embeddedin does not allow you to search for a specific person, the search string becomes the Template:Persondata.

The best way I've found to get only people from Wikipedia is to use list=search and filter the search using AND"Born"AND"Occupation":

http://en.wikipedia.org/w/api.php?action=query&list=search&srsearch="Tom Cruise"AND"Born"AND"Occupation"&format=jsonfm&srprop=snippet&srlimit=50`

Remember that Wikipedia is using a search engine that doesn't yet allow us to search only the title, it will search the full text. You can take advantage of that to get more precise results.




回答3:


The accepted answer explains how to list pages using a certain template, but if you need to search for pages using the template, you can with the hastemplate: search keyword: https://en.wikipedia.org/w/api.php?action=query&list=search&srsearch=hastemplate:NPOV%20physics



来源:https://stackoverflow.com/questions/4043003/does-the-wikipedia-api-support-searches-for-a-specific-template

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