使用PHP抓取Google搜索 [英] Crawling Google Search with PHP

查看:271
本文介绍了使用PHP抓取Google搜索的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图解决如何使用PHP或JavaScript获取Google搜索结果。我知道这是可能的,但现在我找不到方法。

我试图复制(有点)

的功能 http://www.getupdated.se/sokmotoroptimering/seo-verktyg/kolla-排名/



但我真正想要解决的核心问题只是通过PHP或JavaScript获取搜索结果,剩下的我可以弄清楚。 / p>

使用file_get_contents()或cURL获取结果似乎没有用。



示例:

  $ ch = curl_init(); 
$ timeout = 5;
curl_setopt($ ch,CURLOPT_URL,'http://www.google.se/#hl=sv&q=dogs');
curl_setopt($ ch,CURLOPT_RETURNTRANSFER,1);
curl_setopt($ ch,CURLOPT_CONNECTTIMEOUT,$ timeout);
$ result = curl_exec($ ch);
curl_close($ ch);
echo'< pre>';
var_dump($ result);
echo'< / pre>';

结果:


字符串(219)302已移动文档已移至此处。


=http://code.google.com/apis/customsearch/v1/overview.html =nofollow> http://code.google.com/apis/customsearch/v1/overview.html 但这似乎只适用于为一个或多个网站生成自定义搜索。
这似乎需要一个自定义搜索引擎cx参数传递。



因此,无论如何,有什么想法?

解决方案

我之前做过。通过使 https://www.google.co.in/search?hl=zh-CN&output=search&q=india http请求生成html内容,现在解析特定内容使用htmldom php库的标签。您可以使用 PHP SIMPLE HTML DOM 解析结果页面的内容


DEMO:下面的代码会给你所有结果的标题:



 <?php 

include(simple_html_dom.php);

$ html = file_get_html('http://www.google.co.in/search?hl=zh-CN&output=search&q=india');

$ i = 0;
foreach($ html-> find('li [class = g]')as $ element){
foreach($ element-> find('h3 [class = r]')as $ h3)
{
$ title [$ i] ='< h1>'。$ h3->明文。'< / h1>';
}
$ i ++;
}
print_r($ title);

?>


I trying to get my head around how to fetch Google search results with PHP or JavaScript. I know it has been possible before but now I can't find a way.

I am trying to duplicate (somewhat) the functionality of
http://www.getupdated.se/sokmotoroptimering/seo-verktyg/kolla-ranking/

But really the core issue I want to solve is just to get the search result via PHP or JavaScript,the rest i can figure out.

Fetching the results using file_get_contents() or cURL doesn't seem to work.

Example:

$ch = curl_init();
$timeout = 5;
curl_setopt($ch, CURLOPT_URL, 'http://www.google.se/#hl=sv&q=dogs');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
$result = curl_exec($ch);
curl_close($ch);
echo '<pre>';
var_dump($result);
echo '</pre>';

Results:

string(219) "302 Moved The document has moved here."

So, with some Googling i found http://code.google.com/apis/customsearch/v1/overview.html but that seems to only work for generating a custom search for one or more websites. It seem to require a "Custom Search Engine" cx-parameter passed.

So anyway, any idea?

解决方案

I did it earlier. Generate the html contents by making https://www.google.co.in/search?hl=en&output=search&q=india http request, now parse specific tags using the htmldom php library. You can parse the content of result page using PHP SIMPLE HTML DOM

DEMO : Below code will give you title of all the result :

<?php

include("simple_html_dom.php");

$html = file_get_html('http://www.google.co.in/search?hl=en&output=search&q=india');

$i = 0;
foreach($html->find('li[class=g]') as $element) {
    foreach($element->find('h3[class=r]') as $h3) 
    {
        $title[$i] = '<h1>'.$h3->plaintext.'</h1>' ;
    }
       $i++;
}
print_r($title);

?>

这篇关于使用PHP抓取Google搜索的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆