php 正则 抓取网页(php正则抓取特定标签具有特定属性值的get_data)
优采云 发布时间: 2022-03-18 22:03php 正则 抓取网页(php正则抓取特定标签具有特定属性值的get_data)
学了几天php正则,抓了一些网站的数据,发现写正则每次都重新抓起来很麻烦,所以想写个通用接口抓取具有特定属性值的特定标签。 ,直接上代码。
//$html-待搜索的字符串 $tag-待搜索的标签 $attr-待搜索属性的名称 $value-待搜索属性的值
函数get_tag_data($html,$tag,$attr,$value){
$regex = "/(.*?)/is";
回显 $regex。”
";
preg_match_all($regex,$html,$matches,PREG_PATTERN_ORDER);
返回 $matches[1];
}
//返回值是一个数组。下面以found标签中的内容为例。
header("Content-type: text/html; charset=utf-8");
$temp = '';
$result = get_tag_data($temp,"a","class","fc01");
var_dump($result);输出结果为
array(6) { [0]=> string(6) "Home" [1]=> string(6) "Log" [2]=> string(6) "LOFTER" [3]=> string(6) "专辑" [4]=> string(6) "博友" [5]=> string(9) "关于me" }查看源码看看
数组(6) {
[0]=>