首页 > 解决方案 > php curl获取页面上的img src值

问题描述

我想在页面上获取 img src 值,如果它是 https://www.google.com ,那么结果将类似于 https://www.google.com/images/branding/googlelogo/1x/googlelogo_color_272x92dp.png https: //www.google.com/ff.png https://www.google.com/idk.jpg 我想要这样的东西!谢谢

标签: phpcurl

解决方案


<?php

# Use the Curl extension to query Google and get back a page of results
$url = "https://www.google.com";
$ch = curl_init();
$timeout = 5;
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
$html = curl_exec($ch);
curl_close($ch);

# Create a DOM parser object
$dom = new DOMDocument();

# Parse the HTML from Google.
# The @ before the method call suppresses any warnings that
# loadHTML might throw because of invalid HTML in the page.
@$dom->loadHTML($html);

# Iterate over all the <a> tags
foreach($dom->getElementsByTagName('img') as $link) {
        # Show the <a href>
        echo $link->getAttribute('src');
        echo "<br />";
}
?>

这里是


推荐阅读