php - 如果元素有这个或那个类,如何使用 getElementsByTagName()?
问题描述
我有很多元素<img>
,我只想用“A”类或“B”类(而不是“C”类的元素)来扩展它们。
我想这样做是为了用 PHP 解析网页。
这是我的代码:
$doc = file_get_html('https://www.example.com');
foreach($doc-> getElementById('content')-> getElementsByTagName('img') as $item){
}
我如何用.A或.Bimg
类推断?
foreach($doc-> getElementById('content')-> getElementsByTagName('img[class="A" OR "B"]') as $item){
}
非常感谢您的帮助!
解决方案
怎么样find()
?
foreach($doc->getElementById('content')->find("img[class='A'][class='B']") as $item)
{
// other code goes here
}
推荐阅读
- php - 如何获得shopee api授权使用php5.3
- r - 复杂数字字符串提取
- firebase - 所有文档的通用 Firestore 触发器
- android - 当应用程序在运行模式下启动时,Canvas 对象为空,而在调试模式下运行时对象具有值
- android - 运行 Android 模拟器 - 分段错误
- html - HTML表格垂直对齐文本
- r - 计算两列日期之间的天数和变量值
- python - 如何将 Z3_ast 导出为二进制文件以及如何在其中搜索 func 名称?
- java - 如何使用蒙版网格创建自定义按钮?
- c# - 有没有办法使用表达式从 SQLite.net 查询的模型中获取数据?