
等程序运行完成(看右下角),就创建好了!
若是pom.xml文件第一行报错:Unknown,则将Spring Boot Version改小,可以改成2.1.4.RELEASE。
三、编写数据库连接,日志输出
将resource文件夹下的application.properties改成application.yml,并加入logback-spring.xml文件。
application.yml
如上图所示,要扣除里面的表格数据,该怎么弄呢?思路是先把整个表格抠出来,然后按照tr进行分割,存到数组,然后一个个遍历进行正则匹配,最后输出结果转化为json格式,供前台调用!
<?phpclass getHtmlChar {
var $url var $pattern_find var $pattern_replace var $timeout /**
* @param url 需要抓取的url链接
* @param pattern_find 需要匹配的字段正则表达式
* @param pattern_replace 需要替换的字符正则表达式
* @param timeout 设置超时阻断程序执行,默认10秒,这个字段一般留空
*/
function __construct($url, $pattern_find, $pattern_replace, $timeout) {
$this->url = $url $this->pattern_find = $pattern_find $this->pattern_replace = $pattern_replace $this->timeout = 10
} // 获取网页源代码
function getHtml() {
$ch = curl_init()
curl_setopt($ch, CURLOPT_URL, $this->url)
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1)
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 6.1WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/34.0.1847.131 Safari/537.36')//模拟浏览器,防止被拦截
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $this->timeout) $html = curl_exec($ch) // 匹配需要的大的字段,例如一段table,取出来之后再一行一行做匹配
preg_match($this->pattern_find, $html, $match) //判断是否有传入替换的正则表达式,如果有就进行匹配替换,并返回替换后的字符串,如果没有,则直接返回匹配出来的大段,例如整个table表格
// echo "<pre>"
// print_r($match)
// echo "</pre>"
// exit
if (!empty($this->pattern_replace)) { return preg_replace($this->pattern_replace, '', $match[0])
} else { return $match[0]
}
} // 传入正则和需要处理的字符串,返回的是匹配到的数组
function prgMatche($pre_all, $strs) {
if (!is_string($strs)) { echo '传入的不是字符串!请检查!<br>' echo "<pre>"
print_r($strs) echo "</pre>" exit
}
preg_match_all($pre_all, $strs, $matches) return $matches
} // 获取表格表头
function getTableJson() {
// 获取一行一行tr
$trs = $this->prgMatche('/<tr([\s\S]+?)<\/tr>/i', $this->getHtml()) // 获取表头
$ths = $this->prgMatche('/<th>([\s\S]+?)<\/th>/i', $trs[1][0]) // 获取行数,循环匹配的时候需要用到
$num = sizeof($trs[1]) // echo $num
// 一行一行匹配表格数据
for ($i = 1$i <$num$i++) { $tr = $this->prgMatche('/td>([\s\S]+?)<\/td>/i', $trs[1][$i]) $tds[] = $tr[1]
} // 把表头和表数据存入json
array_unshift($tds, $ths[1]) // 返回一个数组,0为json格式数据,1为数组格式数据
return [json_encode($tds), $tds]
}
}// 以下是调用这个类的例子$url = 'http://www.pm25.in/chaozhou'$pattern_find = '/<table([\s\S]+?)<\/table>/i'$pattern_replace = '/\s{2,}|\n|<br>| class="O3_8h_dn"/i'$gtd = new getHtmlChar($url, $pattern_find, $pattern_replace, '')$time_start = microtime(true)echo "<pre>"
print_r($gtd->getTableJson())echo "</pre>"$time_end = microtime(true)
printf("这段程序运行的时间为 %s 秒!", $time_end - $time_start)?>
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)