php获取新浪微博热搜和百度热搜
时间:2022年03月26日
/来源:网络
/编辑:佚名
PHP获取百度关键词排名第一种方法:
<?php
@$k=$_GET['k'] ? $_GET['k'] : '78模板';
@$u=$_GET['u'] ? $_GET['u'] : 'www.78moban.com';
$rn='50';
$url = "https://www.baidu.com/s?ie=utf-8&wd=".$k."&rn=".$rn;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.1 Safari/537.11');
$res = curl_exec($ch);
$rescode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
preg_match_all('/(.*?)/si', $res, $p);
for ($ii = 0; $ii < COUNT($p[1]); $ii++){
$pos=strpos($p[1][$ii],$u);
if(is_integer($pos)){
$rn= $ii+1;
break;
}
}
echo $rn;
?>
php获取百度热搜第二种方法:
<?php
$url = 'https://top.baidu.com/buzz?b=1';
$content = file_get_contents($url);
$pattern_neirong = '/<div.*class="c-single-text-ellipsis">(.*)<\/div>.*/isU';
preg_match_all($pattern_neirong, $content, $match_neirong);
//$match_neirong = mb_convert_encoding($match_neirong, 'UTF-8', 'GBK');
$pattern_souvalue = '/<div.*class="hot-index_1Bl1a">(.*)<\/div>.*/isU';
preg_match_all($pattern_souvalue, $content, $match_souvalue);
function float_number($number)
{
$length = strlen($number); //数字长度
if ($length > 10) { //亿单位
$str = substr_replace(floor($number * 0.0000001), '.', -1, 0) . "亿";
} elseif ($length > 6) { //万单位
//截取前俩为
$str = floor($number * 0.001) * 0.1 . "万";
} else {
return $number;
}
return $str;
}
foreach ($match_souvalue[1] as $key => $value) {
$match_souvalue[1][$key] = float_number($value);
}
?>
php利用curl函数获取新浪微博热搜实例
简要说明
php利用curl函数请求新浪微博提供的热搜接口,返回json数据,利用php json_decode函数将json数据转换为php数组,利用php foreach函数将数组循环显示
code
<?php
$weather = curl_init();
curl_setopt($weather,CURLOPT_URL,"https://api.oioweb.cn/api/summary.php");
curl_setopt($weather, CURLOPT_SSL_VERIFYPEER, false); //如果接口URL是https的,我们将其设为不验证,如果不是https的接口,这句可以不用加
curl_setopt($weather,CURLOPT_RETURNTRANSFER,true);
$data = curl_exec($weather);
curl_close($weather);
$data=json_decode($data,true);//将json格式转化为数组格式,方便使用
if(is_array($data)){
$i=0;
foreach($data as $val){
if($i==9){
break;
}
echo "<a href=".$val['link'].">".$val['title']."</a>",'</br>';
$i++;
}
}
else {
echo "这不是一个数组";
}
?>
<?php
@$k=$_GET['k'] ? $_GET['k'] : '78模板';
@$u=$_GET['u'] ? $_GET['u'] : 'www.78moban.com';
$rn='50';
$url = "https://www.baidu.com/s?ie=utf-8&wd=".$k."&rn=".$rn;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.1 Safari/537.11');
$res = curl_exec($ch);
$rescode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
preg_match_all('/(.*?)/si', $res, $p);
for ($ii = 0; $ii < COUNT($p[1]); $ii++){
$pos=strpos($p[1][$ii],$u);
if(is_integer($pos)){
$rn= $ii+1;
break;
}
}
echo $rn;
?>
php获取百度热搜第二种方法:
<?php
$url = 'https://top.baidu.com/buzz?b=1';
$content = file_get_contents($url);
$pattern_neirong = '/<div.*class="c-single-text-ellipsis">(.*)<\/div>.*/isU';
preg_match_all($pattern_neirong, $content, $match_neirong);
//$match_neirong = mb_convert_encoding($match_neirong, 'UTF-8', 'GBK');
$pattern_souvalue = '/<div.*class="hot-index_1Bl1a">(.*)<\/div>.*/isU';
preg_match_all($pattern_souvalue, $content, $match_souvalue);
function float_number($number)
{
$length = strlen($number); //数字长度
if ($length > 10) { //亿单位
$str = substr_replace(floor($number * 0.0000001), '.', -1, 0) . "亿";
} elseif ($length > 6) { //万单位
//截取前俩为
$str = floor($number * 0.001) * 0.1 . "万";
} else {
return $number;
}
return $str;
}
foreach ($match_souvalue[1] as $key => $value) {
$match_souvalue[1][$key] = float_number($value);
}
?>
php利用curl函数获取新浪微博热搜实例
简要说明
php利用curl函数请求新浪微博提供的热搜接口,返回json数据,利用php json_decode函数将json数据转换为php数组,利用php foreach函数将数组循环显示
code
<?php
$weather = curl_init();
curl_setopt($weather,CURLOPT_URL,"https://api.oioweb.cn/api/summary.php");
curl_setopt($weather, CURLOPT_SSL_VERIFYPEER, false); //如果接口URL是https的,我们将其设为不验证,如果不是https的接口,这句可以不用加
curl_setopt($weather,CURLOPT_RETURNTRANSFER,true);
$data = curl_exec($weather);
curl_close($weather);
$data=json_decode($data,true);//将json格式转化为数组格式,方便使用
if(is_array($data)){
$i=0;
foreach($data as $val){
if($i==9){
break;
}
echo "<a href=".$val['link'].">".$val['title']."</a>",'</br>';
$i++;
}
}
else {
echo "这不是一个数组";
}
?>
新闻资讯 更多