使用 MediaWiki API,this 为我提供了这样的输出,用于搜索术语 Tiger
https://simple.wikipedia.org/w/api.php?action=query&prop=extracts&titles=Tiger&format=json&exintro=1
回应:
{"batchcomplete":"","query":{"pages":{"9796":{"pageid":9796,"ns":0,"title":"Tiger","extract":"<p>The <b>tiger</b> (<i>Panthera tigris</i>) is a carnivorous mammal. It is the largest living member of the cat family, the Felidae. It lives in Asia, mainly India, Bhutan, China and Siberia.</p>\n<p></p>"}}}}
如何获得输出
老虎(Panthera tigris)是一种肉食性哺乳动物。它是猫科动物中现存最大的成员。它生活在亚洲,主要是印度、不丹、中国和西伯利亚。
请问有人可以告诉我如何将所有内容存储在文本文件中吗?我是这里的初学者,所以请友善。我需要这个来完成我在 Raspberry Pi 2 上使用 Bash 和 Raspbian 进行的项目
jq
% jq -r '.query.pages[].extract' file
<p>The <b>tiger</b> (<i>Panthera tigris</i>) is a carnivorous mammal. It is the largest living member of the cat family, the Felidae. It lives in Asia, mainly India, Bhutan, China and Siberia.</p>
<p></p>
要删除 HTML 标签,您可以执行以下操作:
... | sed 's/<[^>]*>//g'
这将删除不在连续行上的 HTML 标签:
% jq -r '.query.pages[].extract' file | sed 's/<[^>]*>//g'
The tiger (Panthera tigris) is a carnivorous mammal. It is the largest living member of the cat family, the Felidae. It lives in Asia, mainly India, Bhutan, China and Siberia.
file
是存储JSON的文件,例如:
curl -so - 'https://simple.wikipedia.org/w/api.php?action=query&prop=extracts&titles=Tiger&format=json&exintro=1' > file
jq '...' file
或
jq '...' <(curl -so - 'https://simple.wikipedia.org/w/api.php?action=query&prop=extracts&titles=Tiger&format=json&exintro=1')
您可以安装
jq
:
sudo apt-get install jq
对于示例输入,您还可以将
grep
与 -P
(PCRE) 一起使用。但建议使用上述正确的 JSON 解析器
grep -oP '(?<=extract":").*?(?=(?<!\\)")' file
<p>The <b>tiger</b> (<i>Panthera tigris</i>) is a carnivorous mammal. It is the largest living member of the cat family, the Felidae. It lives in Asia, mainly India, Bhutan, China and Siberia.</p>\n<p></p>
如果您使用 PHP,则可以相当轻松地完成,如下所示。
我们知道文本存储在
extract
属性内,因此我们需要访问它。
最简单的方法是将 API 中的字符串解析为对象格式,这是通过 PHP 中的
json_decode
方法完成的。然后,您可以从该对象访问 extract
属性,这将为您提供字符串。代码会是这样的:
//Get the string from the API, however you've already done it
$JSONString = getFromAPI();
//Use the inbuilt method to create a JSON object
$JSONObject = json_decode($JSONString);
//Follow the structure to get the pages property
$Pages = JSONObject->query->pages;
//Here, we don't know what the Page ID is (because the MediaWiki API returns a different number, depending on the page)
//Therefore we need to simply get the first key, and within it should be our desired 'extract' key
$Extract = "";
foreach($Pages as $value) {
$Extract = $value->extract;
break;
}
//$Extract now contains our desired text
现在我们需要将
$Extract
的内容写入文件,正如您提到的。这可以通过利用 file_put_contents
方法来完成。
//Can be anything you want
$file = 'APIResult.txt';
// Write the contents to the file,
// using the LOCK_EX flag to prevent anyone else writing to the file at the same time
file_put_contents($file, $Extract, LOCK_EX);
啊我们就完成了!
文档
这些函数(
json_decode
和 file_put_contents
)的文档可以在以下位置找到:
您可能会发现 pamdoc 很有帮助,来自 http://pamdoc.org/ - 它可以理解多种输入文件格式(包括 Mediawiki),并且还具有多种输出文件格式(包括纯文本)。这更像是“瑞士军刀”方法,并且由于 Mediawiki 解析起来非常复杂,您将需要使用类似这样经过大型测试套件的方法。