用 phpQuery 配合 file_get_contents()和 curl 获取网易新闻标题 都不成功,想问下大佬们怎么解决 - V2EX
V2EX = way to explore
V2EX 是一个关于分享和探索的地方
Sign Up Now
For Existing Member  Sign In
77sec
V2EX    PHP

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

  •  1
     
  •   77sec Feb 12, 2019 3835 views
    This topic created in 2630 days ago, the information mentioned may be changed or developed.
    <?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("", "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 replies    2019-06-28 09:32:04 +08:00
    77sec
        1
    77sec  
    OP
       Feb 12, 2019
    这是什么情况 百度搜不到
    KasuganoSoras
        2
    KasuganoSoras  
       Feb 12, 2019
    不知道你想干啥
    把网页内容当成网址去 file_get_contents 是什么操作
    GDC
        3
    GDC  
       Feb 12, 2019
    @KasuganoSoras 他 curl 那段是注释掉的
    580a388da131
        4
    580a388da131  
       Feb 12, 2019 via iPhone
    newDocumentFile($link)
    580a388da131
        5
    580a388da131  
       Feb 12, 2019 via iPhone
    哦 忘了
    pg("title")
    pinerge
        6
    pinerge  
       Mar 2, 2019
    <?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  
       Jun 28, 2019
    问题在于缺少关键 header 吧,像百度和 QQ 一样,你用空头去访问,是没有内容的。
    About     Help     Advertise     Blog     API     FAQ     Solana     4168 Online   Highest 6679       Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 37ms UTC 05:25 PVG 13:25 LAX 22:25 JFK 01:25
    Do have faith in what you're doing.
    ubao msn snddm index pchome yahoo rakuten mypaper meadowduck bidyahoo youbao zxmzxm asda bnvcg cvbfg dfscv mmhjk xxddc yybgb zznbn ccubao uaitu acv GXCV ET GDG YH FG BCVB FJFH CBRE CBC GDG ET54 WRWR RWER WREW WRWER RWER SDG EW SF DSFSF fbbs ubao fhd dfg ewr dg df ewwr ewwr et ruyut utut dfg fgd gdfgt etg dfgt dfgd ert4 gd fgg wr 235 wer3 we vsdf sdf gdf ert xcv sdf rwer hfd dfg cvb rwf afb dfh jgh bmn lgh rty gfds cxv xcv xcs vdas fdf fgd cv sdf tert sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf shasha9178 shasha9178 shasha9178 shasha9178 shasha9178 liflif2 liflif2 liflif2 liflif2 liflif2 liblib3 liblib3 liblib3 liblib3 liblib3 zhazha444 zhazha444 zhazha444 zhazha444 zhazha444 dende5 dende denden denden2 denden21 fenfen9 fenf619 fen619 fenfe9 fe619 sdf sdf sdf sdf sdf zhazh90 zhazh0 zhaa50 zha90 zh590 zho zhoz zhozh zhozho zhozho2 lislis lls95 lili95 lils5 liss9 sdf0ty987 sdft876 sdft9876 sdf09876 sd0t9876 sdf0ty98 sdf0976 sdf0ty986 sdf0ty96 sdf0t76 sdf0876 df0ty98 sf0t876 sd0ty76 sdy76 sdf76 sdf0t76 sdf0ty9 sdf0ty98 sdf0ty987 sdf0ty98 sdf6676 sdf876 sd876 sd876 sdf6 sdf6 sdf9876 sdf0t sdf06 sdf0ty9776 sdf0ty9776 sdf0ty76 sdf8876 sdf0t sd6 sdf06 s688876 sd688 sdf86