[英]Grabbing <tr> with Simple HTML Dom based on inner text of <th>, but <th> has no attributes
I have a HTML which look like this, and I am trying to use simple HTML DOM to grab the Name value and Nickname value. 我有一个看起来像这样的HTML,并且我试图使用简单的HTML DOM来获取Name值和Nickname值。
<tr>.....</tr>
<tr>.....</tr>
<tr>
<th>Name</th>
<td>John</td>
</tr>
<tr>
<th><span>Nickname</span></th>
<td>Johny</td>
</tr>
<tr>.....</tr>
<tr>.....</tr>
I am having difficulties as the th and td does not have id or classes 我遇到困难,因为th和td没有id或class
so i tried using innertext value with the following php code 所以我尝试通过以下php代码使用innertext值
require_once('lib/simple_html_dom.php');
$url='http://123.com';
$html = file_get_html($url);
foreach ( $html->find ( 'tr th' ) as $element ) {
if ($element->innertext = 'Name'){
$element = $element->parent;
$tinfo = $element->find ( 'td', 0 );
$info = $tinfo->plaintext;
echo $info;}
}
$html = file_get_html($url);
foreach ( $html->find ( 'tr th span' ) as $element ) {
if ($element->innertext = 'Nickname'){
$element = $element->parent;
$tinfo = $element->find ( 'td', 0 );
$info = $tinfo->plaintext;
echo $info;}
}
I could not seems to get the above code to work any advice on this? 我似乎无法获得上面的代码以对此有所建议? How do i accomplish this? 我该如何完成?
After removing some conflicting scripts, My new code, as advised by Ghost, now works for scraping name and nickname value 删除了一些冲突的脚本后,按照Ghost的建议,我的新代码现在可以用于抓取名称和昵称值
<?php
$name = $nickname ='';
foreach($html->find('tr') as $tr) { // each row
foreach($tr->childNodes() as $tdh) { // each cell of that row
if($tdh->tag == 'th' && $tdh->innertext == 'Name') {
$name = $tdh->next_sibling()->innertext;
}
}
}
echo $name;
foreach($html->find('tr th') as $tr) { // each row
foreach($tr->childNodes() as $tdh) { // each cell of that row
if($tdh->tag == 'span' && $tdh->innertext == 'Nickname') {
$nickname = $tdh->parent->next_sibling()->innertext;
}
}
}
echo $nickname;
?>
Credit to Ghost for the answer. 归功于Ghost。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.