本人网站上有一个头条文章页内容的抓取功能
========================================

        function https_request($url, $data = null, $referer = "", $type = "Content-Type: application/json", $cookie_file = "")
{
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, True);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, True); if(!empty($referer)) {
curl_setopt($curl, CURLOPT_REFERER, $referer);  
} if ($data != null)
{
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($curl, CURLOPT_POST, 1);
if(!empty($data)) {
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
} $arr = array();
$arr[] = 'Content-Length: ' . strlen($data);
if(!empty($type))
{
$arr[] = $type;
}
curl_setopt($curl, CURLOPT_HTTPHEADER,  $arr);
} curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
if(!empty($cookie_file)) {
curl_setopt($curl, CURLOPT_COOKIEFILE, $cookie_file);
curl_setopt($curl, CURLOPT_COOKIEJAR, $cookie_file);
//echo $cookie_file;
}
$output = curl_exec($curl);
curl_close($curl);
return $output;
}
这个功能代码一直可用,上上周的时候发现不能用了,已抓取不到页面的内容,返回的内容如下:
========================================================
<!DOCTYPE html><html lang=en><head><meta charset=UTF-8><meta name=viewport content="width=device-width,initial-scale=1"><meta http-equiv=X-UA-Compatible content="ie=edge"><link rel="shortcut icon" href=//s3a.pstatp.com/toutiao/resource/ntoutiao_web/static/image/favicon_5995b44.ico type=image/x-icon><title>今日头条</title></head><body><p></p></body></html>body里面是空的。
试了用基它方法,比如
==========================================
$content = file_get_contents($url);

$readContent = fopen($url,"rb");
$content = stream_get_contents($readContent);
fclose($readContent);也不行。请教各位大大们,还有什么其它方法可以抓取到头条文章的内容吗?
示例头条URL:https://m.toutiao.com/i6744865260068422156/?utm_source=xiaochengxu&w2atif=1&channel=news_hot在线等,给200分。

解决方案 »

  1.   

    你是要做爬虫吧   头条对UA的验证比较严格  而且不建议直接抓取内容这样效率太低
    你可以抓一下同类网站的api  我的博文里面有一篇是抓的东方头条的文章   直接可以爬下来所有文章 包括分类的所有的api
      

  2.   

    可以用的时候他内容应该是纯静态的,现在应该是通过js动态加载写入页面内容,光靠curl不能抓到js完全加载后的页面内容
      

  3.   

    我不是要爬它里面的所有内容,而只是针对里面比较感兴趣的文章来手动抓取。现在不能抓了,不知有没有什么其它办法可以实现吗?
    各们大大有试过用我上面发的那个URL来抓取到内容的吗?