php抓取网页指定内容(php正则抓取特定标签具有特定属性值的get_data)

优采云 发布时间: 2021-09-10 15:10

  php抓取网页指定内容(php正则抓取特定标签具有特定属性值的get_data)

  学了几天php正则,抓了一些网站数据,发现写正则又每次都重新抓很麻烦,所以想写一个通用的接口,用具体属性值,直接上传代码。

  //$html-searched string $tag-searched tag $attr-searched 属性名 $value-searched 属性值

  函数 get_tag_data($html,$tag,$attr,$value){

  $regex = "/(.*?)/is";

  echo $regex。"

  ";

  preg_match_all($regex,$html,$matches,PREG_PATTERN_ORDER);

  返回 $matches[1];

  }

  //返回值为数组中找到的标签内容

  下面会给出一个例子

  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)“关于我”}

  查看源代码即可查看

  数组(6){

  [0]=>

0 个评论

要回复文章请先登录注册


官方客服QQ群

微信人工客服

QQ人工客服


线