我想提出一个脚本,它单独工作,但我正在寻找像对应的图像的所有细节alt
,title
而image src
细节。我写了这个:
$websitelink='http://www.wallsave.com/wallpaper/1920x1200/houses-colorful-walpapers-history-156851.html';
$str = file_get_contents($websitelink);
if(strlen($str)>0) {
preg_match_all("/< *img[^>]*src *= *[\"\']?([^\"\']*)/i",$str,$src);
preg_match_all("/< *img[^>]*alt *= *[\"\']?([^\"\']*)/i",$str,$alt);
preg_match_all("/< *img[^>]*title *= *[\"\']?([^\"\']*)/i",$str,$title);
foreach ($src[1] as $srci) {
$imgsrc=$srci.'</br>';
}
foreach ($alt[1] as $alti) {
$altimg=$alti.'</br>';
}
foreach ($title[1] as $titlei) {
$titleimg= $titlei.'</br>';
}
$alt_title_src=$altimg.$imgsrc.$titleimg.'</br>';
echo $alt_title_src;
}
它没有任何作用,否则xpath会更好。
我建议这样做的最佳方法是通过DOMDocument
。为了清楚起见,这是我的代码,使用您的最终变量名称:
// Set the URL value.
$url = 'http://www.wallsave.com/wallpaper/1920x1200/houses-colorful-walpapers-history-156851.html';
// Get the contents of the HTML file & load it into a variable.
$html = file_get_contents($url);
// Parse the HTML with 'DOMDocument()'
$dom = new DOMDocument();
@$dom->loadHTML($html);
// Get all the image tags.
$imgs = $dom->getElementsByTagName('img')->item(0);
$alt_title_src_array = array();
foreach ($dom->getElementsByTagName('img') as $values) {
// Get the image 'src' value.
$imgsrc = $values->attributes->getNamedItem("src")->value;
// Get the image 'alt' value.
$altimg = $values->attributes->getNamedItem("alt")->value;
// Get the image 'title' value.
$titleimg = $values->attributes->getNamedItem("title")->value;
$alt_title_src_array[] = $altimg . $imgsrc . $titleimg . '</br>';
}
// Dump the '$alt_title_src_array' for debugging.
echo '<pre>';
print_r($alt_title_src_array);
echo '</pre>';
运行此命令时,我得到的输出是:
Array
(
[0] => http://www.wallsave.com/thumb/houses-colorful-walpapers-history-156851.jpg
[1] => Follow Me on Pinteresthttp://passets-cdn.pinterest.com/images/follow-on-pinterest-button.png
[2] => http://platform.tumblr.com/v1/share_1.png
[3] => Wallpapers Houses Colorful Walpapers History 1920x1200http://www.wallsave.com/wallpapers/1920x1200/houses/156851/houses-colorful-walpapers-history-156851.jpgHouses Colorful Walpapers History Wallpaper
[4] => http://www.wallsave.com/thumb/houses-d-house-nature-hd-jootix-279025.jpg
[5] => http://www.wallsave.com/thumb/houses-in-the-village-nature-landscape-and-996677.jpg
[6] => http://www.wallsave.com/thumb/houses-sea-villas-hd-942695.jpg
[7] => http://www.wallsave.com/thumb/houses-tree-house-beautiful-picture-259493.jpg
[8] => http://www.wallsave.com/thumb/houses-of-parliament-and-big-ben-london-city-pictures-593315.jpg
[9] => http://www.wallsave.com/thumb/houses-taipei-house-paper-images-buildings-610526.jpg
[10] => http://www.wallsave.com/thumb/houses-wood-house-mountain-and-lake-895032.jpg
[11] => http://www.wallsave.com/thumb/houses-man-red-house-sidewalk-snow-trees-winter-us-811843.jpg
[12] => http://www.wallsave.com/thumb/houses-dr-house-63348.jpg
[13] => http://www.wallsave.com/thumb/houses-of-parliament-london-united-kingdom-city-121730.jpg
[14] => http://www.wallsave.com/thumb/houses-colorful-walpapers-history-156851.jpg
[15] => http://www.wallsave.com/thumb/houses-village-for-free-1876649.jpg
)
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句