1
我想從維基百科獲得用戶輸入公司的摘要段落。如何獲得針對不同公司的維基百科的摘要?
因此,例如,如果用戶輸入Google,我需要顯示Google的摘要段落。
我使用目前的代碼:
// action=parse: get parsed text
// page=$input
// format=json: in json format
// prop=text: send the text content of the article
// section=0: top content of the page
$url = 'http://en.wikipedia.org/w/api.php?action=parse&page=$input&format=json&prop=text§ion=0';
$ch = curl_init($url);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt ($ch, CURLOPT_USERAGENT, "TestScript"); // required by wikipedia.org server; use YOUR user agent with YOUR contact information. (otherwise your IP might get blocked)
$c = curl_exec($ch);
$json = json_decode($c);
$content = $json->{'parse'}->{'text'}->{'*'}; // get the main text content of the query (it's parsed HTML)
// pattern for first match of a paragraph
$pattern = '#<p>(.*)</p>#Us'; // http://www.phpbuilder.com/board/showthread.php?t=10352690
if(preg_match($pattern, $content, $matches))
{
// print $matches[0]; // content of the first paragraph (including wrapping <p> tag)
print strip_tags($matches[1]); // Content of the first paragraph without the HTML tags.
}
哪些工作,如果$input = "Zynga"
但如果$input = "Google"
因爲它返回 「:[4]的參考。」
看來你應該在DOM中尋找一個'
回答
您可以使用
action=query&prop=extracts
替代exintro=
選項。例如。 https://en.wikipedia.org/w/api.php?action=query&prop=extracts&format=json&exlimit=1&exintro=&explaintext=&titles=Google Google。這應該刪除大多數不需要的格式,例如引用。
來源
2014-10-09 23:13:58
相關問題