用 PHP 抓取网页照片 [英] Web scraping photo in PHP

查看:31
本文介绍了用 PHP 抓取网页照片的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果url是用样式写的,我如何从网站复制照片的url.

<a href="http://goruzont.blogspot.com/2017/04/blog-post_6440.html" style="background:url(https://1.bp.blogspot.com/-6vpIH5iqPYs/WPzlNdxsRpI/AAAAAAAntU/d7U_Ch_6FiIPwosNL4tWwqBeXw8qwo2nACLcB/s1600/1424051.jpg) 无重复中心中心;背景尺寸:封面"><span class="thumb-overlay"></span></a>

解决方案

您可以使用正则表达式仅提取背景图片或 background:url 格式

$str='<div class="thumb"><a href="http://goruzont.blogspot.com/2017/04/blog-post_6440.html" style="background:url(https://1.bp.blogspot.com/-6vpIH5iqPYs/WPzlNdxsRpI/AAAAAAAntU/d7U_Ch_6FiIPwosNL4tWwqBeXw8qwo2nACLcB/s1600/1424051.jpg) 无重复中心中心;背景尺寸:封面"><span class="thumb-overlay"></span></a>

';preg_match_all('~\bbackground(-image)?\s*:(.*?)\(\s*(\'|")?(?.*?)\3?\s*\)~i',$str,$matches);$images = $matches['image'];foreach($images as $img){回声 $img;}# https://1.bp.blogspot.com/-6vpIH5iqPYs/WPzlNdxsRpI/AAAAAAAAntU/d7U_Ch_6FiIPwosNL4tWwqBeXw8qwo2nACLcB/s1600/1424051.jpg

How do I copy the url for the photo from the site if the url written in styles.

<div class="thumb">
               <a href="http://goruzont.blogspot.com/2017/04/blog-post_6440.html" style="background:url(https://1.bp.blogspot.com/-6vpIH5iqPYs/WPzlNdxsRpI/AAAAAAAAntU/d7U_Ch_6FiIPwosNL4tWwqBeXw8qwo2nACLcB/s1600/1424051.jpg) no-repeat center center;background-size:cover">

<span class="thumb-overlay"></span></a>
 </div>

解决方案

You can use regex to extract only background image or background:url format

$str=' 
<div class="thumb">
               <a href="http://goruzont.blogspot.com/2017/04/blog-post_6440.html" style="background:url(https://1.bp.blogspot.com/-6vpIH5iqPYs/WPzlNdxsRpI/AAAAAAAAntU/d7U_Ch_6FiIPwosNL4tWwqBeXw8qwo2nACLcB/s1600/1424051.jpg) no-repeat center center;background-size:cover">

<span class="thumb-overlay"></span></a>
 </div>'; 

preg_match_all('~\bbackground(-image)?\s*:(.*?)\(\s*(\'|")?(?<image>.*?)\3?\s*\)~i',$str,$matches);
$images = $matches['image'];
foreach($images as $img){
     echo $img;
}
# https://1.bp.blogspot.com/-6vpIH5iqPYs/WPzlNdxsRpI/AAAAAAAAntU/d7U_Ch_6FiIPwosNL4tWwqBeXw8qwo2nACLcB/s1600/1424051.jpg 

这篇关于用 PHP 抓取网页照片的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆