43

我想用 curl抓取这个谷歌搜索结果页面的内容。我一直在尝试设置不同的用户代理,并设置其他选项,但我似乎无法获取该页面的内容,因为我经常被重定向或出现“页面移动”错误。

我相信这与查询字符串在某处被编码的事实有关,但我真的不确定如何解决这个问题。

    //$url is the same as the link above
    $ch = curl_init();
    $user_agent='Mozilla/5.0 (Windows NT 6.1; rv:8.0) Gecko/20100101 Firefox/8.0'
    curl_setopt ($ch, CURLOPT_URL, $url);
    curl_setopt ($ch, CURLOPT_USERAGENT, $user_agent);
    curl_setopt ($ch, CURLOPT_HEADER, 0);
    curl_setopt ($ch, CURLOPT_FOLLOWLOCATION, 1);
    curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt ($ch,CURLOPT_CONNECTTIMEOUT,120);
    curl_setopt ($ch,CURLOPT_TIMEOUT,120);
    curl_setopt ($ch,CURLOPT_MAXREDIRS,10);
    curl_setopt ($ch,CURLOPT_COOKIEFILE,"cookie.txt");
    curl_setopt ($ch,CURLOPT_COOKIEJAR,"cookie.txt");
    echo curl_exec ($ch);

我需要做什么才能让我的 php 代码显示页面的确切内容,就像我在浏览器上看到的那样?我错过了什么?谁能指出我正确的方向?

我在 SO 上看到过类似的问题,但没有一个可以帮助我的答案。

编辑:

我尝试使用 Selenium WebDriver 打开链接,它提供与 cURL 相同的结果。我仍然认为这与查询字符串中存在特殊字符的事实有关,这些字符在过程中的某个地方被弄乱了。

4

5 回答 5

68

这是如何:

   /**
     * Get a web file (HTML, XHTML, XML, image, etc.) from a URL.  Return an
     * array containing the HTTP server response header fields and content.
     */
    function get_web_page( $url )
    {
        $user_agent='Mozilla/5.0 (Windows NT 6.1; rv:8.0) Gecko/20100101 Firefox/8.0';

        $options = array(

            CURLOPT_CUSTOMREQUEST  =>"GET",        //set request type post or get
            CURLOPT_POST           =>false,        //set to GET
            CURLOPT_USERAGENT      => $user_agent, //set user agent
            CURLOPT_COOKIEFILE     =>"cookie.txt", //set cookie file
            CURLOPT_COOKIEJAR      =>"cookie.txt", //set cookie jar
            CURLOPT_RETURNTRANSFER => true,     // return web page
            CURLOPT_HEADER         => false,    // don't return headers
            CURLOPT_FOLLOWLOCATION => true,     // follow redirects
            CURLOPT_ENCODING       => "",       // handle all encodings
            CURLOPT_AUTOREFERER    => true,     // set referer on redirect
            CURLOPT_CONNECTTIMEOUT => 120,      // timeout on connect
            CURLOPT_TIMEOUT        => 120,      // timeout on response
            CURLOPT_MAXREDIRS      => 10,       // stop after 10 redirects
        );

        $ch      = curl_init( $url );
        curl_setopt_array( $ch, $options );
        $content = curl_exec( $ch );
        $err     = curl_errno( $ch );
        $errmsg  = curl_error( $ch );
        $header  = curl_getinfo( $ch );
        curl_close( $ch );

        $header['errno']   = $err;
        $header['errmsg']  = $errmsg;
        $header['content'] = $content;
        return $header;
    }

例子

//Read a web page and check for errors:

$result = get_web_page( $url );

if ( $result['errno'] != 0 )
    ... error: bad url, timeout, redirect loop ...

if ( $result['http_code'] != 200 )
    ... error: no page, no permissions, no service ...

$page = $result['content'];
于 2013-02-19T09:22:18.870 回答
14

对于模拟大多数人类行为的现实方法,您可能需要在 curl 选项中添加引用者。您可能还想将 follow_location 添加到您的 curl 选项中。相信我,无论谁说无法获取 Google 搜索结果,都是个十足的傻瓜,应该把他/她的电脑靠在墙上,希望再也不会回到 internetz。您可以使用自己的浏览器执行“IRL”的所有操作都可以使用 Python 中的 PHP cURL 或 libCURL 进行模拟。你只需要做更多的cURLS来获得buff。然后你就会明白我的意思了。:)

  $url = "http://www.google.com/search?q=".$strSearch."&hl=en&start=0&sa=N";
  $ch = curl_init();
  curl_setopt($ch, CURLOPT_REFERER, 'http://www.example.com/1');
  curl_setopt($ch, CURLOPT_HEADER, 0);
  curl_setopt($ch, CURLOPT_VERBOSE, 0);
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible;)");
  curl_setopt($ch, CURLOPT_URL, urlencode($url));
  $response = curl_exec($ch);
  curl_close($ch);
于 2013-05-29T18:41:12.167 回答
5

尝试这个:

$url = "http://www.google.com/search?q=".$strSearch."&hl=en&start=0&sa=N";
  $ch = curl_init();
  curl_setopt($ch, CURLOPT_HEADER, 0);
  curl_setopt($ch, CURLOPT_VERBOSE, 0);
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible;)");
  curl_setopt($ch, CURLOPT_URL, urlencode($url));
  $response = curl_exec($ch);
  curl_close($ch);
于 2013-02-19T11:23:16.033 回答
1

我想你有没有注意到你的链接实际上是一个 HTTPS 链接......似乎 CURL 参数不包括任何类型的 SSH 处理......也许这可能是你的问题。为什么不尝试使用非 HTTPS 链接看看会发生什么(即 Google 自定义搜索引擎)...?

于 2014-08-28T23:21:41.773 回答
1

使用 Curl php 获取内容

请求服务器支持 Curl 功能,在 Apache 文件夹中的 httpd.conf 中启用


function UrlOpener($url)
     global $output;
     $ch = curl_init(); 
     curl_setopt($ch, CURLOPT_URL, $url); 
     curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
     $output = curl_exec($ch); 
     curl_close($ch);    
     echo $output;

如果通过 google 缓存使用 Curl 获取内容,您可以使用此 url:http ://webcache.googleusercontent.com/search?q=cache:Put your url Sample:http ://urlopener.mixaz.net/

于 2016-01-09T10:03:21.987 回答