用 PHP 抓取网页照片 [英] Web scraping photo in 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*(\'|")?(? How do I copy the url for the photo from the site if the url written in styles.
You can use regex to extract only background image or background:url format
这篇关于用 PHP 抓取网页照片的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!<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>
$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