查询谷歌搜索引擎?

时间:2015-04-25 22:23:47

标签: php jquery google-chrome

我正在尝试按日期查询Google搜索引擎以获取第一页结果然后处理它。我当前使用的查询返回结果,但不在我设置的日期范围内;如果我将相同的查询复制到谷歌它适用于日期,但不适用于我的PHP脚本。该脚本仅返回当前或正常结果,就像未设置date参数一样。使用的代码段的一部分如下。我所指的查询以及在$ url变量中发布的代码段中。

查询:https://www.google.com/search?q='.$Query.'&source=lnt&tbs=cdr%3A1%2'.$startDate.$EndDate.'&tbm=

$Query= $_POST['Query'];
$Query=str_replace(" ","+",$Query);
if ($_POST['Start_date']==''){
$startday='1';
$startmonth='11';
$startyear='2011';
}
if ($_POST['End_date']==''){
$endday='1';
$endmonth='11';
$endyear='2013';
}
$startDate='Ccd_min%3A'.$startmonth.'%2F'.$startday.'%2F'.$startyear.'.%2';
$EndDate='Ccd_max%3A'.$endmonth.'%2F'.$endday.'%2F'.$endyear.'';

if ($_POST['Query']!=''){
$url  = 'https://www.google.com/search?   
q='.$Query.'&source=lnt&tbs=cdr%3A1%2'.$startDate.$EndDate.'&tbm=';
echo $url .'<p>';
$html = file_get_html($url);
$searchresults=array();
$linkObjs = $html->find('h3.r a');
foreach ($linkObjs as $linkObj) {
$link   = trim($linkObj->href);

    // if it is not a direct link but url reference found inside it, then extract
    if (!preg_match('/^https?/', $link) && preg_match('/q=(.+)&amp;sa=/U', $link, $matches) && preg_match('/^https?/', $matches[1])) {
        $link = $matches[1];
    } else if (!preg_match('/^https?/', $link)) { // skip if it is not a valid link
        continue;
    }
    array_push($searchresults,$link);
}

2 个答案:

答案 0 :(得分:1)

Google为未启用JavaScriptfile_get_html($url))的设备提供了不同的html结构。在chrome上暂时Disable JavaScript并检查页面。这样,您就可以确保在脚本上使用正确的div id'sclasses等。

根据您的评论进行更新:

如果禁用JavaScript,Google不允许通过直接网址按日期范围进行搜索。 尽管如此,您仍然可以使用daterange Google运算符查找在指定日期范围内由Googlebot编制索引的网页。提交的日期必须采用Julian date格式,并且应省略分数以使此运算符正常工作。

Example: daterange:2452671-2452671 lisbon

daterange运算符至少需要一个正确的搜索词,并且可以与其他运算符组合使用。

gregoriantojd()

要将Gregorian date转换为Julian date,您可以使用php函数gregoriantojd( int $month , int $day , int $year ),即:

$startDate = gregoriantojd(12, 28, 2011);
//2455924

$endDate = gregoriantojd(12, 28, 2014);
//2457020

您的搜索$url应如下所示:

$url = "https://www.google.pt/search?q=lisbon+daterange:2455924-2457020&btnG=Search&num=100&gbv=1"

最终代码:

include_once("simple_html_dom.php");

$startDate = gregoriantojd(12, 28, 2011); //2455924
$endDate = gregoriantojd(12, 28, 2014); //2457020
$nResults = "100";
$Query= "lisbon";

$url = "https://www.google.com/search?q=$Query+daterange:$startDate-$endDate&btnG=Search&num=$nResults&gbv=1";

echo $url .'<p>';
$html = file_get_html($url);
$searchresults=array();
$linkObjs = $html->find('h3.r a');
foreach ($linkObjs as $linkObj) {
$link   = trim($linkObj->href);

    // if it is not a direct link but url reference found inside it, then extract
    if (!preg_match('/^https?/', $link) && preg_match('/q=(.+)&amp;sa=/U', $link, $matches) && preg_match('/^https?/', $matches[1])) {
        $link = $matches[1];
    } else if (!preg_match('/^https?/', $link)) { // skip if it is not a valid link
        continue;
    }
    array_push($searchresults,$link);
}
print_r($searchresults);

/*
Array ( [0] => http://www.cnn.com/2014/01/25/travel/lisbon-coolest-city/ [1] => http://www.tripadvisor.com/Tourism-g189158-Lisbon_Lisbon_District_Central_Portugal-Vacations.html
etc...
*/

答案 1 :(得分:0)

您发布的代码中的网址内有换行

$url  = 'https://www.google.com/search?
q='.$Query.'&source=lnt&tbs=cdr%3A1%2'.$startDate.$EndDate.'&tbm=';

Linebreak通常被视为LF个字符(0x0D,类似于unix的系统)或CR + LF字符(0x0D + 0x0A,windows)。

因此,如果您仔细查看您请求的网址,您的脚本会发送一个请求,其中包含%0D%0Aq {}}}的名为q的GET参数。

为了纠正这个问题,你应该将整行两行放在一行上,或者你把linebreak放在字符串文字之外,在你的情况下是每对单个qoutes之间的字符串,例如(第二行开头的点使它更容易不要忽视两条连线):

$url  = 'https://www.google.com/search?q=' 
  . $Query . '&source=lnt&tbs=cdr%3A1%2' . $startDate . $EndDate . '&tbm=';