简体   繁体   English

从HTML页面提取所有文本而不会丢失上下文

[英]Extract all text from a HTML page without losing context

For a translation program I am trying to get a 95% accurate text from a HTML file in order to translate the sentences and links. 对于翻译程序,我试图从HTML文件中获取95%的准确文本,以便翻译句子和链接。

For example: 例如:

<div><a href="stack">Overflow</a> <span>Texts <b>go</b> here</span></div>

Should give me 2 results to translate: 应该给我2个结果翻译:

Overflow

Texts <b>go</b> here

Any suggestions or commercial packages available for this problem? 有任何建议或商业软件包可用于此问题吗?

I'm not exactly sure what you're asking, but look at simplehtmldom . 我不确定您要问什么,但请看一下simplehtmldom Specifically the "Extract Contents from HTML" tab under quick start on that front page (can't link directly, sigh ). 特别是该首页快速入门下的“从HTML提取内容”选项卡( 叹息不能直接链接)。 With that you can extract the text of a website without all those pesky tags. 这样一来,您就可以提取没有所有讨厌标记的网站文本。

使用多个<a>标签后代</a>抓取 HTML 页面<div id="text_translate"><p>我在数据库字段中有这个 html 源代码。 我想分析这段代码,特别是一些表格的字段,并将它们打印在屏幕上。 这是关于表的代码:</p><pre> &lt;table cellspacing="1" cellpadding="1" class="troop_details inReturn" &gt; &lt;thead&gt; &lt;tr&gt; &lt;td class="role"&gt; &lt;a href="/karte.php?d=91628"&gt;01] #WorkInProgress&lt;/a&gt; &lt;/td&gt; &lt;td colspan="11" class="troopHeadline"&gt; &lt;a href="/karte.php?d=91611"&gt;Return from 01-soldier&lt;/a&gt; &lt;/td&gt; &lt;/tr&gt; &lt;/thead&gt; &lt;tbody class="units"&gt; &lt;tr&gt; &lt;th class="coords"&gt; &amp;#x202d;&lt;span class="coordinates coordinatesWrapper coordinatesAligned coordinatesltr"&gt;&lt;span class="coordinateX"&gt;(&amp;#x202d;&amp;minus;&amp;#x202d;1&amp;#x202c;&amp;#x202c;&lt;/span&gt;&lt;span class="coordinatePipe"&gt;|&lt;/span&gt;&lt;span class="coordinateY"&gt;&amp;#x202d;&amp;minus;&amp;#x202d;28&amp;#x202c;&amp;#x202c;)&lt;/span&gt;&lt;/span&gt;&amp;#x202c; &lt;/th&gt; &lt;td class="uniticon"&gt; &lt;img class="unit u21" title="Phalanx: 1:12:51" alt="Phalanx" src="/img/x.gif" /&gt; &lt;/td&gt; &lt;td class="uniticon"&gt; &lt;img class="unit u22" title="Swordsman: 1:25:00" alt="Swordsman" src="/img/x.gif" /&gt; &lt;/td&gt; &lt;td class="uniticon"&gt; &lt;img class="unit u23" title="Pathfinder: 0:30:00" alt="Pathfinder" src="/img/x.gif" /&gt; &lt;/td&gt; &lt;td class="uniticon"&gt; &lt;img class="unit u24" title="Theutates Thunder: 0:26:51" alt="Theutates Thunder" src="/img/x.gif" /&gt; &lt;/td&gt; &lt;td class="uniticon"&gt; &lt;img class="unit u25" title="Druidrider: 0:31:53" alt="Druidrider" src="/img/x.gif" /&gt; &lt;/td&gt; &lt;td class="uniticon"&gt; &lt;img class="unit u26" title="Haeduan: 0:39:14" alt="Haeduan" src="/img/x.gif" /&gt; &lt;/td&gt; &lt;td class="uniticon"&gt; &lt;img class="unit u27" title="Ram: 2:07:30" alt="Ram" src="/img/x.gif" /&gt; &lt;/td&gt; &lt;td class="uniticon"&gt; &lt;img class="unit u28" title="Trebuchet: 2:50:00" alt="Trebuchet" src="/img/x.gif" /&gt; &lt;/td&gt; &lt;td class="uniticon"&gt; &lt;img class="unit u29" title="Chieftain: 1:42:00" alt="Chieftain" src="/img/x.gif" /&gt; &lt;/td&gt; &lt;td class="uniticon"&gt; &lt;img class="unit u30" title="Settler: 1:42:00" alt="Settler" src="/img/x.gif" /&gt; &lt;/td&gt; &lt;td class="uniticon last"&gt; &lt;img class="unit uhero" title="Hero" alt="Hero" src="/img/x.gif" /&gt; &lt;/td&gt; &lt;/tr&gt; &lt;/tbody&gt; &lt;tbody class="units last"&gt; &lt;tr&gt; &lt;th&gt;Troops&lt;/th&gt; &lt;td class="unit none"&gt; 0 &lt;/td&gt; &lt;td class="unit none"&gt; 0 &lt;/td&gt; &lt;td class="unit none"&gt; 0 &lt;/td&gt; &lt;td class="unit"&gt; 500 &lt;/td&gt; &lt;td class="unit none"&gt; 0 &lt;/td&gt; &lt;td class="unit none"&gt; 0 &lt;/td&gt; &lt;td class="unit none"&gt; 0 &lt;/td&gt; &lt;td class="unit none"&gt; 0 &lt;/td&gt; &lt;td class="unit none"&gt; 0 &lt;/td&gt; &lt;td class="unit none"&gt; 0 &lt;/td&gt; &lt;td class="unit none last"&gt; 0 &lt;/td&gt; &lt;/tr&gt; &lt;/tbody&gt; &lt;tbody class="infos"&gt; &lt;tr&gt; &lt;th&gt;Bounty&lt;/th&gt; &lt;td colspan="11"&gt; &lt;div class="res"&gt; &lt;div class="inlineIconList resourceWrapper"&gt;&lt;div class="inlineIcon resources" title="Lumber"&gt;&lt;i class="r1"&gt;&lt;/i&gt;&lt;span class="value "&gt;6758&lt;/span&gt;&lt;/div&gt;&lt;div class="inlineIcon resources" title="Clay"&gt;&lt;i class="r2"&gt;&lt;/i&gt;&lt;span class="value "&gt;8093&lt;/span&gt;&lt;/div&gt;&lt;div class="inlineIcon resources" title="Iron"&gt;&lt;i class="r3"&gt;&lt;/i&gt;&lt;span class="value "&gt;6908&lt;/span&gt;&lt;/div&gt;&lt;div class="inlineIcon resources" title="Crop"&gt;&lt;i class="r4"&gt;&lt;/i&gt;&lt;span class="value "&gt;15741&lt;/span&gt;&lt;/div&gt;&lt;/div&gt; &lt;/div&gt; &lt;div class="carry"&gt; &lt;img class="carry full" title="carry" alt="carry" src="/img/x.gif"/&gt; &amp;#x202d;&amp;#x202d;37500&amp;#x202c;&amp;nbsp;/&amp;nbsp;&amp;#x202d;37500&amp;#x202c;&amp;#x202c; &lt;/div&gt; &lt;/td&gt; &lt;/tr&gt; &lt;/tbody&gt; &lt;tbody class="infos"&gt; &lt;tr&gt; &lt;th&gt;Arrival&lt;/th&gt; &lt;td colspan="11"&gt; &lt;div class="in"&gt;in&amp;nbsp;&lt;span class="timer" counting="down" value="85"&gt;0:01:25&lt;/span&gt;&amp;nbsp;hrs.&lt;/div&gt; &lt;div class="at"&gt;&lt;span&gt;at&amp;nbsp;00:43:10&lt;/span&gt;&lt;span&gt; &lt;/span&gt;&lt;/div&gt; &lt;/td&gt; &lt;/tr&gt; &lt;/tbody&gt; &lt;/table&gt; &lt;a name="at"&gt;&lt;/a&gt; &lt;table cellspacing="1" cellpadding="1" class="troop_details inReturn" &gt; &lt;thead&gt; &lt;tr&gt; &lt;td class="role"&gt; &lt;a href="/karte.php?d=91628"&gt;01] #WorkInProgress&lt;/a&gt; &lt;/td&gt; &lt;td colspan="11" class="troopHeadline"&gt; &lt;a href="/karte.php?d=94829"&gt;Return from 0-New Hulk&lt;/a&gt; &lt;/td&gt; &lt;/tr&gt; &lt;/thead&gt; &lt;tbody class="units"&gt; &lt;tr&gt; &lt;th class="coords"&gt; &amp;#x202d;&lt;span class="coordinates coordinatesWrapper coordinatesAligned coordinatesltr"&gt;&lt;span class="coordinateX"&gt;(&amp;#x202d;&amp;minus;&amp;#x202d;1&amp;#x202c;&amp;#x202c;&lt;/span&gt;&lt;span class="coordinatePipe"&gt;|&lt;/span&gt;&lt;span class="coordinateY"&gt;&amp;#x202d;&amp;minus;&amp;#x202d;28&amp;#x202c;&amp;#x202c;)&lt;/span&gt;&lt;/span&gt;&amp;#x202c; &lt;/th&gt; &lt;td class="uniticon"&gt; &lt;img class="unit u21" title="Phalanx: 0:45:33" alt="Phalanx" src="/img/x.gif" /&gt; &lt;/td&gt; &lt;td class="uniticon"&gt; &lt;img class="unit u22" title="Swordsman: 0:53:09" alt="Swordsman" src="/img/x.gif" /&gt; &lt;/td&gt; &lt;td class="uniticon"&gt; &lt;img class="unit u23" title="Pathfinder: 0:18:46" alt="Pathfinder" src="/img/x.gif" /&gt; &lt;/td&gt; &lt;td class="uniticon"&gt; &lt;img class="unit u24" title="Theutates Thunder: 0:16:47" alt="Theutates Thunder" src="/img/x.gif" /&gt; &lt;/td&gt; &lt;td class="uniticon"&gt; &lt;img class="unit u25" title="Druidrider: 0:19:56" alt="Druidrider" src="/img/x.gif" /&gt; &lt;/td&gt; &lt;td class="uniticon"&gt; &lt;img class="unit u26" title="Haeduan: 0:24:32" alt="Haeduan" src="/img/x.gif" /&gt; &lt;/td&gt; &lt;td class="uniticon"&gt; &lt;img class="unit u27" title="Ram: 1:19:44" alt="Ram" src="/img/x.gif" /&gt; &lt;/td&gt; &lt;td class="uniticon"&gt; &lt;img class="unit u28" title="Trebuchet: 1:46:18" alt="Trebuchet" src="/img/x.gif" /&gt; &lt;/td&gt; &lt;td class="uniticon"&gt; &lt;img class="unit u29" title="Chieftain: 1:03:47" alt="Chieftain" src="/img/x.gif" /&gt; &lt;/td&gt; &lt;td class="uniticon"&gt; &lt;img class="unit u30" title="Settler: 1:03:47" alt="Settler" src="/img/x.gif" /&gt; &lt;/td&gt; &lt;td class="uniticon last"&gt; &lt;img class="unit uhero" title="Hero" alt="Hero" src="/img/x.gif" /&gt; &lt;/td&gt; &lt;/tr&gt; &lt;/tbody&gt; &lt;tbody class="units last"&gt; &lt;tr&gt; &lt;th&gt;Troops&lt;/th&gt; &lt;td class="unit none"&gt; 0 &lt;/td&gt; &lt;td class="unit none"&gt; 0 &lt;/td&gt; &lt;td class="unit none"&gt; 0 &lt;/td&gt; &lt;td class="unit"&gt; 400 &lt;/td&gt; &lt;td class="unit none"&gt; 0 &lt;/td&gt; &lt;td class="unit none"&gt; 0 &lt;/td&gt; &lt;td class="unit none"&gt; 0 &lt;/td&gt; &lt;td class="unit none"&gt; 0 &lt;/td&gt; &lt;td class="unit none"&gt; 0 &lt;/td&gt; &lt;td class="unit none"&gt; 0 &lt;/td&gt; &lt;td class="unit none last"&gt; 0 &lt;/td&gt; &lt;/tr&gt; &lt;/tbody&gt; &lt;tbody class="infos"&gt; &lt;tr&gt; &lt;th&gt;Bounty&lt;/th&gt; &lt;td colspan="11"&gt; &lt;div class="res"&gt; &lt;div class="inlineIconList resourceWrapper"&gt;&lt;div class="inlineIcon resources" title="Lumber"&gt;&lt;i class="r1"&gt;&lt;/i&gt;&lt;span class="value "&gt;6130&lt;/span&gt;&lt;/div&gt;&lt;div class="inlineIcon resources" title="Clay"&gt;&lt;i class="r2"&gt;&lt;/i&gt;&lt;span class="value "&gt;5835&lt;/span&gt;&lt;/div&gt;&lt;div class="inlineIcon resources" title="Iron"&gt;&lt;i class="r3"&gt;&lt;/i&gt;&lt;span class="value "&gt;5638&lt;/span&gt;&lt;/div&gt;&lt;div class="inlineIcon resources" title="Crop"&gt;&lt;i class="r4"&gt;&lt;/i&gt;&lt;span class="value "&gt;12397&lt;/span&gt;&lt;/div&gt;&lt;/div&gt; &lt;/div&gt; &lt;div class="carry"&gt; &lt;img class="carry full" title="carry" alt="carry" src="/img/x.gif"/&gt; &amp;#x202d;&amp;#x202d;30000&amp;#x202c;&amp;nbsp;/&amp;nbsp;&amp;#x202d;30000&amp;#x202c;&amp;#x202c; &lt;/div&gt; &lt;/td&gt; &lt;/tr&gt; &lt;/tbody&gt; &lt;tbody class="infos"&gt; &lt;tr&gt; &lt;th&gt;Arrival&lt;/th&gt; &lt;td colspan="11"&gt; &lt;div class="in"&gt;in&amp;nbsp;&lt;span class="timer" counting="down" value="920"&gt;0:15:20&lt;/span&gt;&amp;nbsp;hrs.&lt;/div&gt; &lt;div class="at"&gt;&lt;span&gt;at&amp;nbsp;00:57:05&lt;/span&gt;&lt;span&gt; &lt;/span&gt;&lt;/div&gt; &lt;/td&gt; &lt;/tr&gt; &lt;/tbody&gt; &lt;/table&gt;</pre><p> 我感兴趣的数据如下:</p><ol><li> 从 01-士兵<strong>归来 00:43:10</strong></li><li> 从 0-新绿巨人<strong>归来 00:57:05</strong></li></ol><p> 感谢您的建议,这是我目前的代码:</p><pre> &lt;?php include 'database.php'?&gt; &lt;?php session_start(); ?&gt; &lt;?php include_once('simple_html_dom.php'); $caserma = $_SESSION["caserma"]; $dom = new DOMDocument; libxml_use_internal_errors(true); $dom-&gt;loadHTML($_SESSION["caserma"], LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD); $xpath = new DOMXPath($dom); $texts = []; foreach ($xpath-&gt;query("//table[contains(@class, 'troop_details') and contains(@class, 'inReturn')]//td[@class='troopHeadline']//a[@href]/text()") as $textNode) { $texts[] = $textNode-&gt;nodeValue; } var_export($texts); ?&gt;</pre><p> 但是作为 output 它给了我数组()</p></div>标签并从特定的文本中提取文本<table> </table> - Scrape HTML page with multiple <table> tags and extract text from specific <a> tag descendants

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 PHP从html页面提取所有文本 - PHP Extract all text from html page 如何从HTML页面提取文本块? - How to extract blocks of text from a HTML page? 从HTML提取文本 - Extract text from HTML 这是否可以仅从HTML文件中提取所有文本? - Is this possible to extract all the text only from a HTML file? 如何使用PHP从HTML文件提取所有文本? - how to extract all text from HTML file using PHP? 如何使用html按钮更改页面而不丢失会话 - How to use html button to change page without losing session 如何在不丢失html标签的情况下获取大文本的一部分? - How to get part of a big text without losing html tags with php? 将HTML表转换为文本文件而不丢失格式 - Convert HTML table into a text file without losing format 将 textarea 中的文本插入 MySQL 数据库而不丢失格式 - Inserting text from textarea into MySQL database without losing formatting 使用多个<a>标签后代</a>抓取 HTML 页面<div id="text_translate"><p>我在数据库字段中有这个 html 源代码。 我想分析这段代码,特别是一些表格的字段,并将它们打印在屏幕上。 这是关于表的代码:</p><pre> &lt;table cellspacing="1" cellpadding="1" class="troop_details inReturn" &gt; &lt;thead&gt; &lt;tr&gt; &lt;td class="role"&gt; &lt;a href="/karte.php?d=91628"&gt;01] #WorkInProgress&lt;/a&gt; &lt;/td&gt; &lt;td colspan="11" class="troopHeadline"&gt; &lt;a href="/karte.php?d=91611"&gt;Return from 01-soldier&lt;/a&gt; &lt;/td&gt; &lt;/tr&gt; &lt;/thead&gt; &lt;tbody class="units"&gt; &lt;tr&gt; &lt;th class="coords"&gt; &amp;#x202d;&lt;span class="coordinates coordinatesWrapper coordinatesAligned coordinatesltr"&gt;&lt;span class="coordinateX"&gt;(&amp;#x202d;&amp;minus;&amp;#x202d;1&amp;#x202c;&amp;#x202c;&lt;/span&gt;&lt;span class="coordinatePipe"&gt;|&lt;/span&gt;&lt;span class="coordinateY"&gt;&amp;#x202d;&amp;minus;&amp;#x202d;28&amp;#x202c;&amp;#x202c;)&lt;/span&gt;&lt;/span&gt;&amp;#x202c; &lt;/th&gt; &lt;td class="uniticon"&gt; &lt;img class="unit u21" title="Phalanx: 1:12:51" alt="Phalanx" src="/img/x.gif" /&gt; &lt;/td&gt; &lt;td class="uniticon"&gt; &lt;img class="unit u22" title="Swordsman: 1:25:00" alt="Swordsman" src="/img/x.gif" /&gt; &lt;/td&gt; &lt;td class="uniticon"&gt; &lt;img class="unit u23" title="Pathfinder: 0:30:00" alt="Pathfinder" src="/img/x.gif" /&gt; &lt;/td&gt; &lt;td class="uniticon"&gt; &lt;img class="unit u24" title="Theutates Thunder: 0:26:51" alt="Theutates Thunder" src="/img/x.gif" /&gt; &lt;/td&gt; &lt;td class="uniticon"&gt; &lt;img class="unit u25" title="Druidrider: 0:31:53" alt="Druidrider" src="/img/x.gif" /&gt; &lt;/td&gt; &lt;td class="uniticon"&gt; &lt;img class="unit u26" title="Haeduan: 0:39:14" alt="Haeduan" src="/img/x.gif" /&gt; &lt;/td&gt; &lt;td class="uniticon"&gt; &lt;img class="unit u27" title="Ram: 2:07:30" alt="Ram" src="/img/x.gif" /&gt; &lt;/td&gt; &lt;td class="uniticon"&gt; &lt;img class="unit u28" title="Trebuchet: 2:50:00" alt="Trebuchet" src="/img/x.gif" /&gt; &lt;/td&gt; &lt;td class="uniticon"&gt; &lt;img class="unit u29" title="Chieftain: 1:42:00" alt="Chieftain" src="/img/x.gif" /&gt; &lt;/td&gt; &lt;td class="uniticon"&gt; &lt;img class="unit u30" title="Settler: 1:42:00" alt="Settler" src="/img/x.gif" /&gt; &lt;/td&gt; &lt;td class="uniticon last"&gt; &lt;img class="unit uhero" title="Hero" alt="Hero" src="/img/x.gif" /&gt; &lt;/td&gt; &lt;/tr&gt; &lt;/tbody&gt; &lt;tbody class="units last"&gt; &lt;tr&gt; &lt;th&gt;Troops&lt;/th&gt; &lt;td class="unit none"&gt; 0 &lt;/td&gt; &lt;td class="unit none"&gt; 0 &lt;/td&gt; &lt;td class="unit none"&gt; 0 &lt;/td&gt; &lt;td class="unit"&gt; 500 &lt;/td&gt; &lt;td class="unit none"&gt; 0 &lt;/td&gt; &lt;td class="unit none"&gt; 0 &lt;/td&gt; &lt;td class="unit none"&gt; 0 &lt;/td&gt; &lt;td class="unit none"&gt; 0 &lt;/td&gt; &lt;td class="unit none"&gt; 0 &lt;/td&gt; &lt;td class="unit none"&gt; 0 &lt;/td&gt; &lt;td class="unit none last"&gt; 0 &lt;/td&gt; &lt;/tr&gt; &lt;/tbody&gt; &lt;tbody class="infos"&gt; &lt;tr&gt; &lt;th&gt;Bounty&lt;/th&gt; &lt;td colspan="11"&gt; &lt;div class="res"&gt; &lt;div class="inlineIconList resourceWrapper"&gt;&lt;div class="inlineIcon resources" title="Lumber"&gt;&lt;i class="r1"&gt;&lt;/i&gt;&lt;span class="value "&gt;6758&lt;/span&gt;&lt;/div&gt;&lt;div class="inlineIcon resources" title="Clay"&gt;&lt;i class="r2"&gt;&lt;/i&gt;&lt;span class="value "&gt;8093&lt;/span&gt;&lt;/div&gt;&lt;div class="inlineIcon resources" title="Iron"&gt;&lt;i class="r3"&gt;&lt;/i&gt;&lt;span class="value "&gt;6908&lt;/span&gt;&lt;/div&gt;&lt;div class="inlineIcon resources" title="Crop"&gt;&lt;i class="r4"&gt;&lt;/i&gt;&lt;span class="value "&gt;15741&lt;/span&gt;&lt;/div&gt;&lt;/div&gt; &lt;/div&gt; &lt;div class="carry"&gt; &lt;img class="carry full" title="carry" alt="carry" src="/img/x.gif"/&gt; &amp;#x202d;&amp;#x202d;37500&amp;#x202c;&amp;nbsp;/&amp;nbsp;&amp;#x202d;37500&amp;#x202c;&amp;#x202c; &lt;/div&gt; &lt;/td&gt; &lt;/tr&gt; &lt;/tbody&gt; &lt;tbody class="infos"&gt; &lt;tr&gt; &lt;th&gt;Arrival&lt;/th&gt; &lt;td colspan="11"&gt; &lt;div class="in"&gt;in&amp;nbsp;&lt;span class="timer" counting="down" value="85"&gt;0:01:25&lt;/span&gt;&amp;nbsp;hrs.&lt;/div&gt; &lt;div class="at"&gt;&lt;span&gt;at&amp;nbsp;00:43:10&lt;/span&gt;&lt;span&gt; &lt;/span&gt;&lt;/div&gt; &lt;/td&gt; &lt;/tr&gt; &lt;/tbody&gt; &lt;/table&gt; &lt;a name="at"&gt;&lt;/a&gt; &lt;table cellspacing="1" cellpadding="1" class="troop_details inReturn" &gt; &lt;thead&gt; &lt;tr&gt; &lt;td class="role"&gt; &lt;a href="/karte.php?d=91628"&gt;01] #WorkInProgress&lt;/a&gt; &lt;/td&gt; &lt;td colspan="11" class="troopHeadline"&gt; &lt;a href="/karte.php?d=94829"&gt;Return from 0-New Hulk&lt;/a&gt; &lt;/td&gt; &lt;/tr&gt; &lt;/thead&gt; &lt;tbody class="units"&gt; &lt;tr&gt; &lt;th class="coords"&gt; &amp;#x202d;&lt;span class="coordinates coordinatesWrapper coordinatesAligned coordinatesltr"&gt;&lt;span class="coordinateX"&gt;(&amp;#x202d;&amp;minus;&amp;#x202d;1&amp;#x202c;&amp;#x202c;&lt;/span&gt;&lt;span class="coordinatePipe"&gt;|&lt;/span&gt;&lt;span class="coordinateY"&gt;&amp;#x202d;&amp;minus;&amp;#x202d;28&amp;#x202c;&amp;#x202c;)&lt;/span&gt;&lt;/span&gt;&amp;#x202c; &lt;/th&gt; &lt;td class="uniticon"&gt; &lt;img class="unit u21" title="Phalanx: 0:45:33" alt="Phalanx" src="/img/x.gif" /&gt; &lt;/td&gt; &lt;td class="uniticon"&gt; &lt;img class="unit u22" title="Swordsman: 0:53:09" alt="Swordsman" src="/img/x.gif" /&gt; &lt;/td&gt; &lt;td class="uniticon"&gt; &lt;img class="unit u23" title="Pathfinder: 0:18:46" alt="Pathfinder" src="/img/x.gif" /&gt; &lt;/td&gt; &lt;td class="uniticon"&gt; &lt;img class="unit u24" title="Theutates Thunder: 0:16:47" alt="Theutates Thunder" src="/img/x.gif" /&gt; &lt;/td&gt; &lt;td class="uniticon"&gt; &lt;img class="unit u25" title="Druidrider: 0:19:56" alt="Druidrider" src="/img/x.gif" /&gt; &lt;/td&gt; &lt;td class="uniticon"&gt; &lt;img class="unit u26" title="Haeduan: 0:24:32" alt="Haeduan" src="/img/x.gif" /&gt; &lt;/td&gt; &lt;td class="uniticon"&gt; &lt;img class="unit u27" title="Ram: 1:19:44" alt="Ram" src="/img/x.gif" /&gt; &lt;/td&gt; &lt;td class="uniticon"&gt; &lt;img class="unit u28" title="Trebuchet: 1:46:18" alt="Trebuchet" src="/img/x.gif" /&gt; &lt;/td&gt; &lt;td class="uniticon"&gt; &lt;img class="unit u29" title="Chieftain: 1:03:47" alt="Chieftain" src="/img/x.gif" /&gt; &lt;/td&gt; &lt;td class="uniticon"&gt; &lt;img class="unit u30" title="Settler: 1:03:47" alt="Settler" src="/img/x.gif" /&gt; &lt;/td&gt; &lt;td class="uniticon last"&gt; &lt;img class="unit uhero" title="Hero" alt="Hero" src="/img/x.gif" /&gt; &lt;/td&gt; &lt;/tr&gt; &lt;/tbody&gt; &lt;tbody class="units last"&gt; &lt;tr&gt; &lt;th&gt;Troops&lt;/th&gt; &lt;td class="unit none"&gt; 0 &lt;/td&gt; &lt;td class="unit none"&gt; 0 &lt;/td&gt; &lt;td class="unit none"&gt; 0 &lt;/td&gt; &lt;td class="unit"&gt; 400 &lt;/td&gt; &lt;td class="unit none"&gt; 0 &lt;/td&gt; &lt;td class="unit none"&gt; 0 &lt;/td&gt; &lt;td class="unit none"&gt; 0 &lt;/td&gt; &lt;td class="unit none"&gt; 0 &lt;/td&gt; &lt;td class="unit none"&gt; 0 &lt;/td&gt; &lt;td class="unit none"&gt; 0 &lt;/td&gt; &lt;td class="unit none last"&gt; 0 &lt;/td&gt; &lt;/tr&gt; &lt;/tbody&gt; &lt;tbody class="infos"&gt; &lt;tr&gt; &lt;th&gt;Bounty&lt;/th&gt; &lt;td colspan="11"&gt; &lt;div class="res"&gt; &lt;div class="inlineIconList resourceWrapper"&gt;&lt;div class="inlineIcon resources" title="Lumber"&gt;&lt;i class="r1"&gt;&lt;/i&gt;&lt;span class="value "&gt;6130&lt;/span&gt;&lt;/div&gt;&lt;div class="inlineIcon resources" title="Clay"&gt;&lt;i class="r2"&gt;&lt;/i&gt;&lt;span class="value "&gt;5835&lt;/span&gt;&lt;/div&gt;&lt;div class="inlineIcon resources" title="Iron"&gt;&lt;i class="r3"&gt;&lt;/i&gt;&lt;span class="value "&gt;5638&lt;/span&gt;&lt;/div&gt;&lt;div class="inlineIcon resources" title="Crop"&gt;&lt;i class="r4"&gt;&lt;/i&gt;&lt;span class="value "&gt;12397&lt;/span&gt;&lt;/div&gt;&lt;/div&gt; &lt;/div&gt; &lt;div class="carry"&gt; &lt;img class="carry full" title="carry" alt="carry" src="/img/x.gif"/&gt; &amp;#x202d;&amp;#x202d;30000&amp;#x202c;&amp;nbsp;/&amp;nbsp;&amp;#x202d;30000&amp;#x202c;&amp;#x202c; &lt;/div&gt; &lt;/td&gt; &lt;/tr&gt; &lt;/tbody&gt; &lt;tbody class="infos"&gt; &lt;tr&gt; &lt;th&gt;Arrival&lt;/th&gt; &lt;td colspan="11"&gt; &lt;div class="in"&gt;in&amp;nbsp;&lt;span class="timer" counting="down" value="920"&gt;0:15:20&lt;/span&gt;&amp;nbsp;hrs.&lt;/div&gt; &lt;div class="at"&gt;&lt;span&gt;at&amp;nbsp;00:57:05&lt;/span&gt;&lt;span&gt; &lt;/span&gt;&lt;/div&gt; &lt;/td&gt; &lt;/tr&gt; &lt;/tbody&gt; &lt;/table&gt;</pre><p> 我感兴趣的数据如下:</p><ol><li> 从 01-士兵<strong>归来 00:43:10</strong></li><li> 从 0-新绿巨人<strong>归来 00:57:05</strong></li></ol><p> 感谢您的建议,这是我目前的代码:</p><pre> &lt;?php include 'database.php'?&gt; &lt;?php session_start(); ?&gt; &lt;?php include_once('simple_html_dom.php'); $caserma = $_SESSION["caserma"]; $dom = new DOMDocument; libxml_use_internal_errors(true); $dom-&gt;loadHTML($_SESSION["caserma"], LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD); $xpath = new DOMXPath($dom); $texts = []; foreach ($xpath-&gt;query("//table[contains(@class, 'troop_details') and contains(@class, 'inReturn')]//td[@class='troopHeadline']//a[@href]/text()") as $textNode) { $texts[] = $textNode-&gt;nodeValue; } var_export($texts); ?&gt;</pre><p> 但是作为 output 它给了我数组()</p></div>标签并从特定的文本中提取文本<table> </table> - Scrape HTML page with multiple <table> tags and extract text from specific <a> tag descendants
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM