用PHP,DOMXpath解析不一致的HTML

时间:2019-07-04 04:54:43

标签: php html-parsing domxpath

我有许多网页需要从中抓取/解析数据。页面的结构是一致的,但是是可变的,并且在将数据放置到数组中很多小时后,我一直没有成功。

以下是HTML的示例:

<div class="cc">
    Explore the use of simple 
    <a class="keyword_w_elaboration">technologies</a>
    <div class="elaboration" style="text-decoration:none">
        <div class="elaboration_controls"></div>
        <div class="elaboration_content">
            <div class="keyword_elaboration_title">technologies</div>
            things that extend human abilities (e.g., axe) &nbsp;
        </div>
    </div> 
    to extend their abilities
</div>  

我想做的是在<div class = "cc">之后得到文本(其中有数百个类名为cc的div)

我需要的文字:探索使用简单技术来扩展其功能

问题:

  1. 有链接<a class="keyword_w_elaboration">hover over me</a> 当鼠标悬停在上方时,弹出该单词或短语的定义/说明。
  2. 我每个人可能有一个或多个“阐述”,它们可能在文本中的任何位置(从头到尾)。
  3. 我需要加入<a class="keyword_w_elaboration">一词,或者 文字短语。
  4. 我需要跟踪详细说明和描述。例如技术:扩展人类能力的事物

代码如下:

$dom = new DOMDocument();
libxml_use_internal_errors(true);
$dom->loadHTMLFile('https://website.com');
$xpath = new \DOMXpath($dom);

foreach($xpath->query('//div[@class="cc"]') as $cc)
{

    // everything in here I can't get to work correctly
}

有什么想法吗?

0 个答案:

没有答案