如何用简单的 HTML DOM 抓取这个 [英] how to scrape this with Simple HTML DOM
问题描述
我正在尝试使用简单的 html dom 从看起来像这样的文件中提取元素.
I'm trying to use simple html dom to extract elements from a file that looks like this.
- 该文件有几个看起来相同的表
class=sometable
. - 每个表都有几个
. - 然后在每个 tr 中,我有具有标题的
th
和具有类别的 td.- The file has several tables that look the same
class=sometable
. - Each table has a few
<tr class=sometr>
. - Then inside each tr, I have
th
that has the title, and a td that has a category.
我要提取的是所有表中所有表行的所有标题
class=title
及其对应的类别号class=category
.我已经在$html
中加载了文件.有人能告诉我在那之后我应该找到什么吗?我什至尝试了$collection = $html->find('tr');
并对集合做了一个 vardump 但什么也没得到,所以看起来我没有选择正确.What I want to extract is all titles
class=title
and their corresponding category numberclass=category
for all table rows in all tables. I've loaded the file in$html
. Can someone tell me what I'm supposed to find after that? I've tried even$collection = $html->find('tr');
and did a vardump on the collection but got nothing, so it looks like I'm not selecting right.<table class="sometable"> <tbody> <tr class="sometr"> <th><a class="title">Table 1 Title1</a></th> <td class="category" id="categ-113"></td> <td class="somename">Table 1 Title 1 name</td> </tr> <tr></tr> <tr></tr> </tbody> </table> <table class="sometable"> </table> <table class="sometable"> </table>
推荐答案
我已经测试了 this 并且它有效
I have tested this and it works
$tables = $dom->find('table[@class="sometable"]'); foreach($tables as $table) { $titles = $table->find('a[@class="title"]'); foreach($titles as $title) { echo "Link title = " . $title ."<br />"; } $categories = $table->find('td[@class="category"]'); foreach($categories as $category) { echo "Category id = " . $category->id ."<br />"; } $titles2 = $table->find('td[@class="somename"]'); foreach($titles2 as $title2) { echo "Title2 = " . $title2 ."<br />"; } }
这篇关于如何用简单的 HTML DOM 抓取这个的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文登录 关闭
扫码关注1秒登录发送“验证码”获取 | 15天全站免登陆 - 然后在每个 tr 中,我有具有标题的