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

2019-02-12 21:50:38 +08:00
 77sec
<?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) ""

我只是想要 网易哒哒:用更短的时间,带你看更酷的世界 这个文章标题
非常感谢
3026 次点击
所在节点    PHP
7 条回复
77sec
2019-02-12 22:11:38 +08:00
这是什么情况 百度搜不到
KasuganoSoras
2019-02-12 22:12:50 +08:00
不知道你想干啥
把网页内容当成网址去 file_get_contents 是什么操作
GDC
2019-02-12 22:48:31 +08:00
@KasuganoSoras 他 curl 那段是注释掉的
580a388da131
2019-02-12 23:17:38 +08:00
newDocumentFile($link)
580a388da131
2019-02-12 23:29:47 +08:00
哦 忘了
pg("title")
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
2019-06-28 09:32:04 +08:00
问题在于缺少关键 header 吧,像百度和 QQ 一样,你用空头去访问,是没有内容的。

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/534335

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX