在昨天完善Magic主题的时候,突然想到一个需求,获取我另一个博客的文章和对应url。但是Halo博客没有提供api(可能是我不知道),但是它提供了sitemap。
那样我就可以用php去解析sitemap然后输出<a>
标签。
使用cURL发送get请求发现回传一个html。
html
1<html lang="zh-CN">
2<head>
3<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
4<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
5<title>荫茵小窝 网站地图</title>
6<meta name="robots" content="index,follow" />
7<style type="text/css">
8..
9</style>
10</head>
11<body>
12<h2 style="text-align: center; margin-top: 20px">荫茵小窝 网站地图 </h2>
13<div id="nav"><a href="https://blog.yiny.ml"><strong>荫茵小窝</strong></a> » <a href="https://blog.yiny.ml/sitemap.html">站点地图</a></div>
14<div id="content">
15<h3>最新文章</h3>
16<ul id="myTable">
17<li>
18<div class="T1-h pull-left">URL</div>
19<div class="T2-h pull-right">Last Change</div>
20<div class="T3-h pull-right">Change Frequency</div>
21<div class="T4-h pull-right">Priority</div>
22</li>
23<div class="myClear"></div>
24<li>
25<div class="T1 pull-left"><a href="https://blog.yiny.ml" title="荫茵小窝">荫茵小窝</a></div>
26<div class="T2 pull-right">2019-02-27</div>
27<div class="T3 pull-right">daily</div>
28<div class="T4 pull-right">1</div>
29</li>
30<div class="myClear"></div>
31<li>
32<div class="T1 pull-left"><a href="https://blog.yiny.ml/archives/折腾失败的新主题" title="折腾失败的新主题">折腾失败的新主题 | 荫茵小窝</a></div>
33<div class="T2 pull-right">2019-02-27</div>
34<div class="T3 pull-right">daily</div>
35<div class="T4 pull-right">0.6</div>
36</li>
COPY
可以发现需要的标题都在 '/archives/.*?" title="(.*?)">
那么我们用这个正则表达式进行解析。
php
1 $url_map = $url . '/sitemap.html';
2 $ch = curl_init();
3 curl_setopt($ch, CURLOPT_URL, $url_map);
4 curl_setopt($ch, CURLOPT_HEADER, 0);
5 curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.121 Safari/537.36');
6 curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
7 $html = curl_exec($ch);
8 curl_close($ch);
9
10 preg_match_all('#<div class="T1 pull-left"><a href="'.$url.'/archives/.*?" title="(.*?)">#', $html, $title);
COPY
首先使用cURL获取html存入$html, 然后使用preg_match_all
正则匹配 把匹配到的放入$title 变量。这里要注意传入preg_match_all中第一个参数是匹配式,需要两端加#
原因未知,反正没加就报错。走了很多弯路。
$title是一个数组,里面会有两个数组,第一个是匹配到的项,第二个是匹配到的结果,这里我们要第二个数组。也就是$title['1']
,并将它倒置,这样可以从新到旧的方式排列。
php
1 $title['1'] = array_reverse($title['1']);
COPY
构造url列表。
php
1foreach ($title['1'] as $item)
2{
3 $url_list[] = $url.'/archives/'.$item;
4}
COPY
然后构造<a>
标签。使用array_map()方法。
php
1$all = array_map(function ($i1, $i2) {
2 return '<a href="' . $i1 . '" target="_blank">' . $i2 . '</a>';
3}, $url_list, $title['1']);
COPY
最后附上完整的方法。
php
1function parse_halo_sitemap($url)
2{
3 $url_map = $url . '/sitemap.html';
4 $ch = curl_init();
5 curl_setopt($ch, CURLOPT_URL, $url_map);
6 curl_setopt($ch, CURLOPT_HEADER, 0);
7 curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.121 Safari/537.36');
8 curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
9 $html = curl_exec($ch);
10 curl_close($ch);
11
12 preg_match_all('#<div class="T1 pull-left"><a href="'.$url.'/archives/.*?" title="(.*?)">#', $html, $title);
13 $title['1'] = array_reverse($title['1']);
14 $url_list = array();
15 foreach ($title['1'] as $item)
16 {
17 $url_list[] = $url.'/archives/'.$item;
18 }
19
20 $all = array_map(function ($i1, $i2) {
21 return '<a href="' . $i1 . '" target="_blank">' . $i2 . '</a>';
22 }, $url_list, $title['1']);
23 return $all;
24
25}
COPY
其他博客也是如此。
扫码关注腾讯云开发者
领取腾讯云代金券
Copyright © 2013 - 2025 Tencent Cloud. All Rights Reserved. 腾讯云 版权所有
深圳市腾讯计算机系统有限公司 ICP备案/许可证号:粤B2-20090059 深公网安备号 44030502008569
腾讯云计算(北京)有限责任公司 京ICP证150476号 | 京ICP备11018762号 | 京公网安备号11010802020287
Copyright © 2013 - 2025 Tencent Cloud.
All Rights Reserved. 腾讯云 版权所有