【微信小程序】图库——(后台接口篇)
程序员文章站
2022-03-31 10:21:33
...
最近比较闲,所以就打算做些小东西。。。就当练练手,这次做的是一个微信的图库小程序,后台接口用的是TP5框架。。主要思路是后台抓取并返回某个网站所有图片的链接以及其他内容,微信端接收后台返回的json数据,请求之后展示图片
完整项目地址
https://gitee.com/gdhsxuan/personal_tk
小程序二维码:
完成效果
抓取的图片网址是
打开之后随便点一个类别。。。(我这里点的是cos。。其他的都一样。。手动滑稽。。)
F12选中缩略图的部分。。分析下网页结构。。
找到了缩略图的url,图集的url和文字。。
然后看下分页的部分。。。点击下一页。。打开控制台的network选项
发现了需要提交的参数,后来经过多次尝试后发现这个网址可以直接使用get方式提交。。xxx/?paged=xxx
接下来写段代码试试
header("Content-type: text/html; charset=utf-8");
$paged=1;
$ch1 = curl_init();
$url = "http://www.jdlingyu.mobi/collection/cos";
$data1="paged={$paged}";
curl_setopt($ch1, CURLOPT_URL,$url);
curl_setopt($ch1, CURLOPT_POST, 1);
curl_setopt($ch1, CURLOPT_POSTFIELDS, $data1);
curl_setopt($ch1, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch1, CURLOPT_HEADER, 0);
curl_setopt($ch1, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows; U; Windows NT 5.1; zh-CN; rv:1.9.1.5) Gecko/20091102 Firefox/3.5.5');
$result=curl_exec($ch1);
curl_close($ch1);
print_r($result);die;
加上正则
header("Content-type: text/html; charset=utf-8");
$paged=1;
$ch1 = curl_init();
$url = "http://www.jdlingyu.mobi/collection/cos";
$data1="paged={$paged}";
curl_setopt($ch1, CURLOPT_URL,$url);
curl_setopt($ch1, CURLOPT_POST, 1);
curl_setopt($ch1, CURLOPT_POSTFIELDS, $data1);
curl_setopt($ch1, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch1, CURLOPT_HEADER, 0);
curl_setopt($ch1, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows; U; Windows NT 5.1; zh-CN; rv:1.9.1.5) Gecko/20091102 Firefox/3.5.5');
$result=curl_exec($ch1);
curl_close($ch1);
$pat1='/<div style="background-image:url(.*?)" class="preview thumb-in"><\/div>/';
$pat3='/<h2 class="entry-title"><a target="_blank" href="(.*?)" rel="bookmark">(.*?)<\/a><\/h2>/';
preg_match_all($pat1, $result, $m);
preg_match_all($pat3, $result, $z);
print_r($m);print_r($z);die;
接下来是抓每个图集的内容了。。。
分析的方法都差不多。。。就是注意下要过滤掉一些不相关的图片的内容
最后贴上完整的代码。。(有些代码懒得删了。。。)
<?php
namespace app\index\controller;
use think\Controller;
use think\Request;
class Index extends Controller
{
public function index()
{
header("Content-type: text/html; charset=utf-8");
$arr=array();
$all_arr=array();
$paged=Request::instance()->param('page');
//$paged=1;
$ch1 = curl_init();
$url = "http://www.jdlingyu.mobi/collection/cos";
$data1="paged={$paged}";
curl_setopt($ch1, CURLOPT_URL,$url);
curl_setopt($ch1, CURLOPT_POST, 1);
curl_setopt($ch1, CURLOPT_POSTFIELDS, $data1);
curl_setopt($ch1, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch1, CURLOPT_HEADER, 0);
curl_setopt($ch1, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows; U; Windows NT 5.1; zh-CN; rv:1.9.1.5) Gecko/20091102 Firefox/3.5.5');
$result=curl_exec($ch1);
curl_close($ch1);
$pat1='/<div style="background-image:url(.*?)" class="preview thumb-in"><\/div>/';
$pat3='/<h2 class="entry-title"><a target="_blank" href="(.*?)" rel="bookmark">(.*?)<\/a><\/h2>/';
preg_match_all($pat1, $result, $m);
preg_match_all($pat3, $result, $z);
//print_r($m);print_r($z);die;
foreach ($m[1] as $key => $v) {
$v=str_replace("('",'', $v);
$v=str_replace("')",'', $v);
array_push($arr,$v);
}
$all_arr['thumb']=$arr;
$all_arr['link']=$z[1];
$all_arr['text']=$z[2];
//array_push($all_arr,$arr,$z[1],$z[2]);
$more=array();
// $thumb_arr=array_chunk($all_arr['thumb'],2);
// $link_arr=array_chunk($all_arr['link'],2);
// $text_arr=array_chunk($all_arr['text'],2);
for ($i=0; $i <count($all_arr['link']) ; $i++) {
array_push($more,$all_arr['thumb'][$i],$all_arr['link'][$i],$all_arr['text'][$i]);
}
$s=array_chunk($more,3);
$res=array_chunk($s,2);
return json(['res'=>$res]);
// $this->assign('all',$all_arr);
// return $this->fetch();
}
public function detail()
{
header("Content-type: text/html; charset=utf-8");
$link=Request::instance()->param('link');
$arr=array(); //用来存储出现次数最多
$all_arr=array();
$str="";
//$paged="1";
$ch1 = curl_init();
$url = $link;
//$data1="paged={$paged}";
curl_setopt($ch1, CURLOPT_URL,$url);
// curl_setopt($ch1, CURLOPT_POST, 1);
// curl_setopt($ch1, CURLOPT_POSTFIELDS, $data1);
curl_setopt($ch1, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch1, CURLOPT_HEADER, 0);
curl_setopt($ch1, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows; U; Windows NT 5.1; zh-CN; rv:1.9.1.5) Gecko/20091102 Firefox/3.5.5');
$result=curl_exec($ch1);
curl_close($ch1);
$pat1='/<img.*?src=[\"|\']?(.*?)[\"|\']?\s.*?width=.+?>/i';
preg_match_all($pat1, $result, $m);
//print_r($m[1]);
foreach ($m[1] as $k => $v) {
if($v!=="http://wx1.sinaimg.cn/large/d030806aly1ftlwthq2uyj20ci0iqn5m.jpg")
{
$str.=strlen($v).',';
$arr=explode(',',$str);
}
}
$arr=array_count_values($arr);
arsort($arr);
$arr=array_keys($arr);
foreach ($m[1] as $key => $value) {
if($value!=="http://wx1.sinaimg.cn/large/d030806aly1ftlwthq2uyj20ci0iqn5m.jpg")
{
if(strlen($value)==$arr[0])
{
array_push($all_arr,$value);
}
}
}
return json(['res'=>$all_arr]);die;
print_r($all_arr);
}
上一篇: 【记录】VMware安装CentOS6.5时遇到的问题和解决方法总结
下一篇: STL之deque