V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
77sec
V2EX  ›  PHP

用 phpQuery 配合 file_get_contents()和 curl 获取网易新闻标题 都不成功,想问下大佬们怎么解决

  •  1
     
  •   77sec · 2019-02-12 21:50:38 +08:00 · 3009 次点击
    这是一个创建于 1892 天前的主题,其中的信息可能已经有所发展或是发生改变。
    <?php

    require('phpQuery/phpQuery.php');

    $link = "http://news.163.com/19/0131/15/E6S1OSOL000189DH.html";

    /*function curl_get($url, $gzip=false){
    $curl = curl_init($url);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 10);
    if($gzip) curl_setopt($curl, CURLOPT_ENCODING, "gzip"); // 关键在这里
    $content = curl_exec($curl);
    curl_close($curl);
    return $content;
    }

    $content = curl_get($link,$gzip=false);*/

    $content = file_get_contents($link);

    $content = iconv("gb2312", "utf-8//IGNORE", $content);

    phpQuery::newDocumentFile($content); //以 html 内容的方式进行初始化

    $title = pq(".post_content_main h1")->text();

    var_dump($title);
    ?>


    获取之后,显示如下
    Warning: file_get_contents(<!DOCTYPE HTML> <!--[if IE 6 ]> <html id="ne_wrap" class="ne_ua_ie6 ne_ua_ielte8"> <![endif]--> <!--[if IE 7 ]> <html id="ne_wrap" class="ne_ua_ie7 ne_ua_ielte8"> <![endif]--> <!--[if IE 8 ]> <html id="ne_wrap" class="ne_ua_ie8 ne_ua_ielte8"> <![endif]--> <!--[if IE 9 ]> <html id="ne_wrap" class="ne_ua_ie9"> <![endif]--> <!--[if (gte IE 10)|!(IE)]><!--> <html id="ne_wrap"> <!--<![endif]--> <head> <title>网易哒哒:用更短的时间,带你看更酷的世界_网易新闻</title> <base target="_blank"/><!--[if lte IE 6]></base><![endif]--> <meta http-equiv="expires" content="0"/> <meta http-equiv="Cache-Control" content="no-transform"/> <meta http-equiv="Cache-Control" cont in D:\xin\phpStudy\PHPTutorial\www\php_learn\phpquery\phpQuery\phpQuery.php on line 408
    string(0) ""

    我只是想要 网易哒哒:用更短的时间,带你看更酷的世界 这个文章标题
    非常感谢
    7 条回复    2019-06-28 09:32:04 +08:00
    77sec
        1
    77sec  
    OP
       2019-02-12 22:11:38 +08:00
    这是什么情况 百度搜不到
    KasuganoSoras
        2
    KasuganoSoras  
       2019-02-12 22:12:50 +08:00
    不知道你想干啥
    把网页内容当成网址去 file_get_contents 是什么操作
    GDC
        3
    GDC  
       2019-02-12 22:48:31 +08:00
    @KasuganoSoras 他 curl 那段是注释掉的
    580a388da131
        4
    580a388da131  
       2019-02-12 23:17:38 +08:00 via iPhone
    newDocumentFile($link)
    580a388da131
        5
    580a388da131  
       2019-02-12 23:29:47 +08:00 via iPhone
    哦 忘了
    pg("title")
    pinerge
        6
    pinerge  
       2019-03-02 09:41:51 +08:00
    <?php

    $res = curl('http://news.163.com/19/0131/15/E6S1OSOL000189DH.html');

    echo $res;

    function curl($url)
    {
    $ch = curl_init();
    $options = [
    CURLOPT_URL => $url,
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_AUTOREFERER => true,
    CURLOPT_FOLLOWLOCATION => true,
    // CURLOPT_SSL_VERIFYHOST => 0,
    // CURLOPT_SSL_VERIFYPEER => false,
    // CURLOPT_TIMEOUT_MS => 20000,
    // CURLOPT_REFERER => 'https://alpha.wallhaven.cc/wallpaper/' . $id,
    // CURLOPT_HTTPHEADER => [
    // 'user-agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.109 Safari/537.36',
    // ],
    ];
    curl_setopt_array($ch, $options);
    $res = curl_exec($ch);
    curl_close($ch);
    preg_match('#<title>([\W\w]+)_网易新闻</title>#', iconv('gbk', 'utf-8', $res), $matches);
    return $matches[1];
    }
    xpxw
        7
    xpxw  
       2019-06-28 09:32:04 +08:00
    问题在于缺少关键 header 吧,像百度和 QQ 一样,你用空头去访问,是没有内容的。
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   5263 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 44ms · UTC 08:38 · PVG 16:38 · LAX 01:38 · JFK 04:38
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.