简单的HTML Dom刮刮Google结果

时间:2014-08-11 20:06:37

标签: php html xml dom web-scraping

作为"知识图表的一部分,我需要抓取Google返回任何查询的一小段文字。它来自维基百科的结果(一般在右侧)。这样我就可以将纯文本转换为语音答案。使用简单的HTML Dom我从Bing或Ask中抓取这样的信息没有问题,但是这个结果嵌套在Google上的非常DIV(和SPAN),我只是无法得到它。简单的功能如下:

$question = str_replace(' ','+',$_GET['question']);
$address = 'http://www.google.co.uk/search?q='.$question;
$ret = scraping_Google($address);

function scraping_Google($url) {
    // create HTML DOM
    $html = file_get_html($url);

    // get title
    $ret = $html->find('div.kno-rdesc', 0)->plaintext;

    // clean up memory
    $html->clear();
    unset($html);

    return $ret;
}

echo $ret;

非常 div.kno-rdesc 是内容嵌套的地方(我可以使用Chrome上的Code Inspector轻松检索)。然而,解析这一小部分信息并没有成功。有人能帮忙吗?干杯!

1 个答案:

答案 0 :(得分:0)

你不需要刮掉它。谷歌有一个API。 Tap into the power of Google's Knowledge Graph with Freebase data