如何检查HTML img属性是否有特定的文本并抓取它
How to check if HTML img attribute has certain text and scrape it
基本上我有一个远程网页我需要获取它的内容像这样:
$src = file_get_contents('http://example.com/comp.html');
$text = new domDocument;
@$text->loadHTML($src); // I read on a separate post that the '@' supresses warnings
$text->preserveWhiteSpace = false;
comp.html页面如下所示
<html>
<div id = "test1">
<img src = "http://example.com/monkey"/>
</div
<div id = "test2">
<img src = "http://example.com/apples"/>
</div>
</html>
我想获得div id 'test2'的图像源,但忽略test1,因为它不会在条件中。然后,我想在test2 'http://example.com/apples'中抓取img src的字符串,并将其分开,只获取.com/之后的任何值,因此,如果网站是'http://example.com/oranges',它将获得值'橙子'。然后我想把这个值存储到一个变量中。
为了做到这一点,我在上面的代码后面写了这样的东西:$text2 = $text->getElementsByTagName('img');
foreach ($text2 as $image) {
$image->getAttribute('src');
// My question is after this, how would I proceed?
}
按此进行…
$dom = new DOMDocument;
$dom->loadHTML($html);
foreach ($dom->getElementsByTagName('div') as $dtag) {
if ($dtag->getAttribute('id') === 'test2') {
foreach($dtag->getElementsByTagName('img') as $itag)
echo basename($itag->getAttribute('src')); //"prints" apples
}
}
相关文章:
- 在Python中抓取javascript渲染的文本的最快解决方案
- 使用 jQuery 从选择标签中抓取文本
- 使用 jQuery 将 HTML 文本抓取到 JSON 中,但由于循环引用而无法字符串化
- Beautifulsoup抓取数据,其中有js文本在中间
- 如何使用rangy从iframe中抓取选定的文本
- 使用 casperjs 抓取文本节点的最快方法
- 如何分析 XML 文件和抓取文本值
- 从 URL 中抓取文本和媒体
- Rails 3 - 从文本区域中识别属性并抓取它(如果可用)
- 如何抓取页面上的某些文本
- 如何从URL中抓取文本并放置在JS数组中
- JS从文本框中抓取文本,传递给asp.net mvc ActionResult,但ActionResult参数显示为nu
- React Native TextInput抓取高亮显示的文本,并在我点击时保持高亮显示
- 如何在事件onclick中抓取文本
- 使用 jQuery 抓取跨度文本并作为查询传递
- 使用Python和Selenium抓取Javascript文本
- 使用Python从网页中抓取Javascript文本
- 如何从文本src抓取文本
- 使用Javascript/Jquery抓取特定模式的文本
- JavaScript中的文本抓取