纽约时报文章api函数
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了纽约时报文章api函数相关的知识,希望对你有一定的参考价值。
<?php //ini_set('display_errors',3); //error_reporting(E_ALL & ~E_NOTICE & ~E_WARNING); //NYT_query("json","javascript","[article search key]",false); // with the 4th parameter set to false , it will only print out the total numbser of articles matching your query. NYT_query("json","jay-z","[article search key]",true,80); NYT_query("json","eminem","[article search key]",true,80); //NYT_query("json","javascript","[article search key]",true,10); //with the 4th parameter set to true, it will print out the total number and also, write each article to file. //the txt files will be in the current working directory under a directory named {your query} in this case it's javascript //some will return zero kb. im not sure what the hell is going on with that(nyt server error status 500), but you can get most articles this way. //the 5th paramter is to specify the maximum number of articles to write to file. This is pretty helpful when queries like "computer" have over 120000 results //in the future i'll probably refine the search paramters a bit to get more accurate results such as the ability to search for all articles containing "computer" in the "technology" section of the NYTimes function NYT_query($format,$query,$apikey,$writetoFile,$max){ $URL = "http://api.nytimes.com/svc/search/v1/article?format=$format&query=$query&api-key=$apikey"; $html = file_get_html($URL); $arr = $result->results; $total = $result->total; echo $total." "; if($writetoFile){ if($max>$total){ $max=$total; } }else{ } for($i=0;$i<$maximum;$i++){ $url= "http://api.nytimes.com/svc/search/v1/article?format=$format&query=$query&offset=$i&api-key=$apikey"; $html = file_get_html($url); $array = $result->results; foreach($array as $t){ $url = $t->url; writeToFile("archive/".$query."/".$title.".txt",$artBody); }else{ writeToFile("archive/".$query."/".$title.".txt",$artBody); } } } } } function extractArticle($url){ $html = file_get_html($url); $body=""; foreach($html->find('.articleBody') as $element){ $body.= $element->plaintext." "; } $html->clear(); return $body; } function writeToFile($filename,$body){ $myFile = $filename; $stringData = $body; }
以上是关于纽约时报文章api函数的主要内容,如果未能解决你的问题,请参考以下文章
通过API获取统计信息时报Access denied错误处理记录
jsp-api.jarJsp页面调用pageContext.setAttribute()方法时报红
理解使用SpringBoot+Mybatis时,并发请求API时报指针异常
编写AMAZON API程序时报错‘MarketplaceWebService_Client‘
android studio运行时报错AVD Nexus_5X_API_P is already running解决办法