简体   繁体   English

从文本和html标签中检测并提取图像URL

[英]Detect and extract image url from text and html tags

How can i detect if there is some image html tag inside a text and extract just the url of the image ? 如何检测文本中是否存在某些图像html标记并仅提取图像的网址?

Eg. 例如。

Extract this url : 提取此网址:

http://
www.someurl.com/somefileprocessor.php/12345/somedir/somesubdir/someniceimage.j
pg

from this tag (this tag can be inside another bunch of text and/or html) 来自此标记(此标记可以在另一堆文本和/或html中)

<img title="Some nice title" border="0"
hspace="0" alt="some useful hint" src="http://
www.someurl.com/somefileprocessor.php/12345/somedir/somesubdir/someniceimage.j
pg" width="629" height="464" />

Thank's in advance Ângelo 感谢提前Ângelo

A quick attempt at an <img/> tag specific regex: 快速尝试<img/>标记特定的正则表达式:

preg_match_all('/<img[^>]*?\s+src\s*=\s*"([^"]+)"[^>]*?>/i', $str, $matches);

Example

You can use CRUL to get content and then extract all img tags from content. 您可以使用CRUL获取内容,然后从内容中提取所有img标记。 to get data by curl : 通过curl获取数据:

function get_data($url) {
    $ch = curl_init();
    $timeout = 5;
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
    $data = curl_exec($ch);
    curl_close($ch);
    return $data;
}

then use regular expression to extract data. 然后使用正则表达式提取数据。

^https?://(?:[a-z\-]+\.)+[a-z]{2,6}(?:/[^/#?]+)+\.(?:jpg|gif|png)$

this helps you to extract all image urls(in img tag or not). 这有助于您提取所有图像网址(在img标签中或不是)。

If you need crawler ,you can use PHPCrawl 如果您需要抓取工具,可以使用PHPCrawl

Thank's a lot for the awnswers, as i learn some more PHP. 感谢awnswers,因为我学习了更多的PHP。 I try this quick and dirty way, it also extracts the image url 我尝试这种快速而肮脏的方式,它也提取图像网址

$imageurl    = strstr($title, 'src',FALSE);
$imageurl    = strstr($imageurl,'"',FALSE);
$imageurlpos = strpos($imageurl,'"');
$imageurl    = substr($imageurl,$imageurlpos+1);
$imageurlpos = strpos($imageurl,'"');
$imageurl    = substr($imageurl,0,$imageurlpos);

使用多个<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.

相关问题 从rss feed中的html标签提取文本 - Extract text from html tags in an rss feed 如何检测文本中的HTML标签? - How to detect HTML tags in text? 从html字符串中检测节点并在PHP中提取图像 - Detect node from html string and extract image inside in PHP PHP-从网址中提取标签 - PHP - Extract a tags from a url 检测图像URL并包装IMG标签? - Detect image URL and wrap in IMG tags? PHP-如果仅包含HTML标记或带有HTML标记的文本,则检测字符串 - PHP - Detect string if contains only HTML tags or text with HTML tags 从HTML提取文本 - Extract text from HTML 使用DOM从网址中提取HTML - Extract html from url with DOM 使用多个<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 替换HTML文本中的所有图像src标签 - replacing all image src tags in HTML text
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM