php抓取网页标签(如何可能难到程序猿的图片采集方式怎么可能猿呢 )

优采云 发布时间: 2022-03-31 06:05

  php抓取网页标签(如何可能难到程序猿的图片采集方式怎么可能猿呢

)

  有时候我们有这样的需求,在一系列的html页面中,提取整个img标签,或者提取img标签中的src属性,也就是我们需要的图片链接,不多说直接粘贴代码,代码如下:

  function extract_img($tag) {

preg_match_all('/(id|alt|title|src)=("[^"]*")/i', $tag, $matches);

$ret = array();

foreach($matches[1] as $i => $v) {

$ret[$v] = $matches[2][$i];

}

return $ret;

}

$img_tag = '

';

$atts = extract_img($img_tag);

print_r($atts);

  以下是程序执行后的返回结果:

  返回结果

Array

(

[id] => "logo"

[src] => "http://www.devdo.net/wp-content/uploads/2015/06/2015-06-02.jpg"

[alt] => "我爱吃鱼"

[title] => "我爱吃鱼啊"

)

  下面是我自己用的代码,目的是抓拍某图片站的图片,但是站长对图片做了一些处理采集,但是这种处理方式怎么难程序员呢

  //需要提取的网页代码

$str='

';

function getSimpleImg($str = '')

{

if (!empty($str)) {

$result = preg_replace("/.*]*srcset[=\s\"\']+([^\"\']*)[\"\'].*/", "$1", $str);

} else {

$result = '';

}

}

getSimpleImg($str);

function extract_attrib($tag) {

preg_match_all('/(srcset)=("[^"]*")/i', $tag, $matches);

// print_r($matches[2]);

$ret = array();

$img='';

foreach($matches[2] as $i => $v) {

if ($i%2==0) {

//$img.=""."";

$v= str_replace("-200x200","",$v);

$v=str_replace("-300x300","",$v);

echo &#39;<img src=&#39;."/spanspan class="token variable"$v/span" span class="token punctuation"./spanspan class="token single-quoted-string string"&#39;/>&#39;;

}

}

return $img;

}

$atts = extract_attrib($str);

print_r($atts);

0 个评论

要回复文章请先登录注册


官方客服QQ群

微信人工客服

QQ人工客服


线