gpt4 book ai didi

php - DOM 解析 - 如何解析这样的文件中的 html

转载 作者:行者123 更新时间:2023-12-03 22:10:05 24 4
gpt4 key购买 nike

我想从 dom 中提取从带有 curl 请求的外部服务器获得的文件的文本。我将请求放在变量 calle $html_response 中。

我开始了

$dom = new DOMDocument;
$dom->loadHTML($html_response);

但是我将如何提取该文件末尾的文本? (向下滚动显示)
<html>
<head>
<title></title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>

<body>
<font style="text-decoration:none; font-family: Arial; font-size: 40px; color: #b4b4b4; eight: 35px;">HI</font>
<div class="toolbar">
</div>
<style type="text/css">
body,
td,
th {
color: #000000;
}
body {
background-color: #eeeeee;
}
</style>
<meta name="viewport" content="width=device-width">
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<form method="post" action="">
<p><b>YES YOU CAN</b>
<input type="text" size="12" maxlength="14" name="named" value="">
<input type="submit" value="a" name="submit"><br>
</p>
<p><span class="center"><b>PT1</b></span></p>
<p><span class="center"><b>PT2</b> <br>
<b>XXXXXXXX bold</b> other words!</span></p>
</form><br> other words
<font size="5" face="monospace" color="Black">other words</font>
<br><br>


CANT' TAKE THIS PART BECOUSE THERE ISN'T A TAG THAT CLOSE THIS TEXT
AND I'M WORKING ON A EXTERNAL WEBSITE


</body>
</html>

谢谢!

最佳答案

您可以通过使用像这样的 DOM php 解析器来为您的隐匿体提供响应文本:

$dom = new DOMDocument;
$dom->loadHTML($html_response);

// retrieve text node at 'body' level with XPath
$xpath = new DOMXpath($dom);
$textNodes = $xpath->query('/html/body/child::text()');

// filter the nodes' content to retrieve the most pertinent ones (here, remove empty texts)
$texts = array();
foreach($textNodes as $node)
{
if( strlen($node->nodeValue) > 0)
$texts[] = $node->nodeValue ;
}

// get the latest text, as what you need is always at the bottom of the page
echo end($texts); // CANT' TAKE THIS PART BECOUSE THERE ISN'T A TAG THAT CLOSE THIS TEXT AND I'M WORKING ON A EXTERNAL WEBSITE

或者如果有 2 <br><br> ,则简单地分解代码:
$whatryousearchingfor = explode('<br><br>',$html_response)[1];

良好的编码 2 U

关于php - DOM 解析 - 如何解析这样的文件中的 html,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54962364/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com