小编gri*_*gon的帖子

PHP 简单 HTML DOM 解析器:如何获取包含 <h1> 标签的父 div 的内容?

我正在抓取(使用 PHP 简单 HTML DOM)许多不同的(新闻)网站,目的是获取页面上的主要内容/文本正文。

要做到这一点,我能想到的最好方法是找到主标题/标题 (H1) 并获取与此标题标记相同的 div 中包含的文本。

在下面的两个示例中,我将如何获取整个(父级?)div 的内容。

<div>  <----- need to get contents of this whole div (containing the h1 and likely the main body of text)
  <h1></h1>
  main body of text here
</div>
Run Code Online (Sandbox Code Playgroud)

Div 可能在树的更上面。

<div> <----- need to get contents of this whole div
  <div>   
    <h1></h1>
  </div>

  <div>
    main body of text here
  </div>
</div>
Run Code Online (Sandbox Code Playgroud)

在树上进一步划分。

<div> <----- need to get contents of this whole div
  <div>

    <div>   
      <h1></h1>
    </div>

    <div>
      main body …
Run Code Online (Sandbox Code Playgroud)

php simple-html-dom web-scraping simpledom

3
推荐指数
1
解决办法
6703
查看次数

标签 统计

php ×1

simple-html-dom ×1

simpledom ×1

web-scraping ×1