我有这么多的网站地址有我想要的图像。 我想知道该网站的图片来源。如何从其他网站获取图片来源?
以下是我的php代码。但那不起作用。
<?php
$html = array('url1', 'url2', ...);
$result = "";
preg_match_all('/<img[^>]+>/i', $html, $result);
echo $result;
?>
请您填写上面不完整的代码吗?
我有这么多的网站地址有我想要的图像。 我想知道该网站的图片来源。如何从其他网站获取图片来源?
以下是我的php代码。但那不起作用。
<?php
$html = array('url1', 'url2', ...);
$result = "";
preg_match_all('/<img[^>]+>/i', $html, $result);
echo $result;
?>
请您填写上面不完整的代码吗?
您最好使用DOMDocument
类,千万不要使用Regex
作为解析HTML内容的解析器。
$htmlsourceofthewebsite = file_get_contents('http://www.somewebsite.com');
$dom = new DOMDocument;
$dom->loadHTML($htmlsourceofthewebsite);
foreach ($dom->getElementsByTagName('img') as $tag) {
echo $tag->getAttribute('src');
}
}
我在哪里插入我想搜索的网站地址?我是初学者,所以对不起 – user3183995
<?php $ htmlsourceofthewebsite ='http://www.w3schools.com'; $ dom = new DOMDocument; $ dom-> loadHTML($ htmlsourceofthewebsite); ($ dom-> getElementsByTagName('img')as $ tag){ echo $ tag-> getAttribute('src'); } } ?> – user3183995
不,请参阅编辑答案。使用'file_get_contents()';上面的代码是 –
你不应该使用正则表达式来解析html内容。使用DOMDocument。
尝试这样的:
$html=array('url1', 'url2', ........); // your url array
foeach($html as $a){ //run a loop through your array
getImage($a); // get images
}
function getImage($url){
$dom = new DOMDocument;
$dom->loadHTML($url);
foreach ($dom->getElementsByTagName('img') as $t) {
echo $t->getAttribute('src');
}
}
}
你如何期望,这将是与此代码的工作? –