开发者

MediaWiki API action=parse fails with "The page parameter cannot be used together with the text and title parameters"

I'm very new to the Wikipedia API, but I recently came across a Chinese website built on top of wikimedia and I would like to use it to help me parse various pages into a workable format for eventual processing with XPATH. After 开发者_如何学Goreading for a bit, I found that the action=parse parameter was what I am looking for. For instance, the following query loads without difficulty: (from Wikipedia)

api.php?action=parse&page=Main_Page&format=xml

It presents the text, followed by language links, followed by links. I am particularly interested in the links section, as I would be using this data to crawl through this wikipedia-based site to create a hierarchy of pages.

Attempting to replicate these results, I tacked the query onto the end of the page for my site:

http://www.youbianku.com/api.php?action=parse&page=%E5%8C%97%E4%BA%AC&format=xml

%E5%8C%97%E4%BA%AC resolves to the chinese characters for Beijing, btw. Anyhow, I get the following result:

<api>
<error code="params" info="The page parameter cannot be used together with the text and title parameters"/>
</api>

All I have done is to replicate the query from Wikipedia and replace the name of the page. It is unclear to me why this has suddenly thrown an error. There is no problem running other API queries on this page, as the following shows:

api.php?action=query&format=xml&titles=%E5%8C%97%E4%BA%AC&rvprop=content&prop=revisions

I read recently that this may be due to htaccess rewrite rules adding a title by default. Is there a way to bypass these, given that I am a client of this website?


As you suggest, this issue is probably caused by a broken URL rewriting rule.

You can work around this problem by using the text parameter and transcluding the page you want, like this:

/api.php?action=parse&text={{:Page_title}}

(The leading : is there to prevent Template: being prepended to the page title by default.)

Trying this with the example page in your question returns a PHP error for some — probably unrelated — reason, but it works fine with other pages on that wiki.

A disadvantage of this trick is that it bypasses the parser cache, making it slower and more resource-consuming than simply using page. Also, any variables used on the page that depend on the page title are likely to yield unexpected results, and any variables depending on page or revision metadata will probably fail entirely. Fortunately, such variables are not used very often in practice.


Another, perhaps even better solution may be to simply use

/index.php?action=render&title=Page_title

which will return the parsed HTML source of the page without any surrounding skin, like this. This method is not as versatile as the API, but it suffers from none of the problems described above.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜