【发布时间】:2015-03-14 20:50:18
【问题描述】:
这是 HTML 页面 (test.html)
<div id = 'mainid'>
<div id = 'subid'>
Name: ABC
</div>
<div id = 'subid'>
Country: USA
</div>
<div id = 'subid'>
Date of birth: 15 Feb 1985
</div>
</div>
<div id = 'mainid'>
<div id = 'subid'>
Name: Jisan
</div>
<div id = 'subid'>
Country: Japan
</div>
<div id = 'subid'>
Date of birth: 15 Feb 1985
</div>
</div>
<div id = 'mainid'>
<div id = 'subid'>
Name: Mr Barman
</div>
<div id = 'subid'>
Country: Canada
</div>
<div id = 'subid'>
Date of birth: 15 Feb 1985
</div>
</div>
这里是 PHP 代码
$file = $DOCUMENT_ROOT. "test.html";
$doc = new DOMDocument();
$doc->loadHTMLFile($file);
$xpath = new DOMXpath($doc);
$Querys = $xpath->query("*//div[@id='mainid']");
foreach ($Querys as $Querys) {
echo $Name = Please help me about this code;
echo $Country = Please help me about this code;
echo $DOB = Please help me about this code;
}
注意:我想得到这样的结果
Name: ABC, Country: USA, Date of birth: 15 Feb 1985.
Name: Jisan, Country: Japan, Date of birth: 15 Feb 1985.
Name: Mr Barman, Country: Canada, Date of birth: 15 Feb 1985.
【问题讨论】:
标签: php xpath web-scraping scraper