php - 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 我想要这样的东西!谢谢
解决方案
<?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 />";
}
?>
这里是
推荐阅读
- appium - Mac Safari 浏览器上的 Appium 执行速度非常慢
- javascript - D3:使用 TopoJSON 网格缩放到边界
- c - 以十六进制逐字节打印打开文件
- ios - GCD:线程只有两种类型吗?主线程和后台线程
- ruby-on-rails - 如何在 Rails 中正确使用基本范围
- python - importing functions from another jupyter notebook
- mobile - 基于移动的内部工具的技术堆栈
- android - 在 android studio 3.0.2 的命令行上打开 android sqlite 数据库文件
- php - 如何在 PHP 中创建一个返回 Json 的 Restful Web 服务
- asterisk - 星号回铃音录音