【发布时间】:2018-01-16 22:32:54
【问题描述】:
如何使用一些具有相同名称的 div 类来抓取页面,以及如何创建 html 表? 这是页面代码:
<div class="date">20/11/2018</div>
<div class="time">12:00</div>
<div class="nation">Italy</div>
<div class="date">20/11/2020</div>
<div class="time">12:00</div>
<div class="nation">England</div>
<div class="date">20/11/2025</div>
<div class="time">13:00</div>
<div class="nation">Spain</div>
我想用抓取的数据创建一个 html 表,例如:
DATE | TIME | NATION
X | X | X
为每个 div 类名。我只能抓取一个 div,这是我的代码,我想让它在 html 页面中的每个 div 类中循环。看看我的代码没有表格代码:
include("simple_html_dom.php");
$html = file_get_contents('https://test.test');
$dom = new DOMDocument();
$dom->loadHTML($html);
$finder = new DomXPath($dom);
$classname = "date";
$nodes = $finder->query("//*[contains(concat(' ', normalize-space(@class), ' '), ' $classname ')]");
$data1 = $nodes{0}->nodeValue;
echo $data1;
【问题讨论】:
-
找到这些元素的父节点对获取这些元素的内容有很大帮助
-
我不想采用父节点,因为在“div”之后我想采用其他“div”,我必须跳过。例如:日期、地点、号码、时间、昵称、国家。我只想要日期、时间和国家。
-
但是找到父节点可以更好地控制找到您想要的节点。
标签: php html web-scraping scrape