Php 从标记角度刮取完整的HTML
假设我有一个HTML页面作为Php 从标记角度刮取完整的HTML,php,html,web-scraping,custom-selectors,Php,Html,Web Scraping,Custom Selectors,假设我有一个HTML页面作为 <p> Some text here </p> <p> Some other text here </p> <h1> Title 1 </h1> <p> Another text here </p> <p> Some random text here </p> <h1> Title 2 </h1> <p> Som
<p> Some text here </p>
<p> Some other text here </p>
<h1> Title 1 </h1>
<p> Another text here </p>
<p> Some random text here </p>
<h1> Title 2 </h1>
<p> Some text here </p>
<p> Some other text here </p>
<h1>..<h1>
这里有一些文本
这里还有其他一些文字
标题1
这里是另一个文本
这里有一些随机文本
标题2
这里有一些文字
这里还有其他一些文字
..
是否可以逐个标记刮取内容的标签
if (<h1>)
then do something
if (<p>)
then do something else
if()
那就做点什么
如果()
那就做点别的吧
对于每个标记,php将按标记名称选择元素。如果将*
放入函数参数中,它将返回所有元素
$dom = new DOMDocument();
$dom->loadHTML($html);
foreach ($dom->getElementsByTagName('*') as $element){
if ($element->tagName == "h1")
// do something
if ($element->tagName == "p")
// do something
}
检查中的结果,所以您要做的是循环遍历所有html元素,并为每个元素执行一些操作?签出Jsoup库()。它有很多方法可以帮助你完成任务needful@RandomDeveloper-是的,我想循环遍历每个标记,然后应用我尝试使用的必要条件,很好地获得标记$article->find('h1')->plaintext可能类似于php中的html标记数组?