现在preg对我来说一直是我喜欢的工具,但是如果我不知道我想做什么是可能的话让我知道如何去做我的头脑
我想要的是preg_match能够返回一个div的innerHTML问题是读取div有更多的div并且我的preg一直关闭它找到的第一个标签
这是我的实际代码
$scrape_address = "http://isohunt.com/torrent_details/133831593/98e034bd6382e0f4ecaa9fe2b5eac01614edc3c6?tab=summary";
$ch = curl_init($scrape_address);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, '1');
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_ENCODING, "");
$data = curl_exec($ch);
preg_match('% <div id="torrent_details">(.*)</div> %six', $data, $match);
print_r($match);
这已针对TomcatExodus的帮助进行了更新
住在:: http://megatorrentz.com/beta/details.php?hash=98e034bd6382e0f4ecaa9fe2b5eac01614edc3c6
答案 0 :(得分:1)
<?php
$scrape_address = "http://isohunt.com/torrent_details/133831593/98e034bd6382e0f4ecaa9fe2b5eac01614edc3c6?tab=summary";
$ch = curl_init($scrape_address);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, '1');
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_ENCODING, "");
$data = curl_exec($ch);
$domd = new DOMDocument();
libxml_use_internal_errors(true);
$domd->loadHTML($data);
libxml_use_internal_errors(false);
$div = $domd->getElementById("torrent_details");
if ($div) {
$dom2 = new DOMDocument();
$dom2->appendChild($dom2->importNode($div, true));
echo $dom2->saveHTML();
} else {
echo "Has no element with the given ID\n";
}
答案 1 :(得分:1)
使用正则表达式会在解析标记文档时经常出现问题。
XPath版本 - 独立于源布局。你唯一需要的是一个带有id的div。
loadHTMLFile($url); $xp = new domxpath($dom); $result = $xp->query("//*[@id = 'torrent_details']"); $div=$result->item(0); if($result->length){ $out =new DOMDocument(); $out->appendChild($out->importNode($div, true)); echo $out->saveHTML(); }else{ echo "No such id"; } ?>
这是Maerlyn解决方案的解决方案。它不起作用,因为getElementById()想要一个指定了id属性的DTD。我的意思是,您始终可以使用“apple”作为记录ID来构建文档,因此您需要一些内容,其中“id”实际上是此标记的ID。
validateOnParse = true; @$domd->loadHTML($data); //this doesn't work as the DTD is not specified //or the specified id attribute is not the attributed called "id" //$div = $domd->getElementById("torrent_details"); /* * workaround found here: https://fosswiki.liip.ch/display/BLOG/GetElementById+Pitfalls * set the "id" attribute as the real id */ $elements = $domd->getElementsByTagName('div'); if (!is_null($elements)) { foreach ($elements as $element) { //try-catch needed because of elements with no id try{ $element->setIdAttribute('id', true); }catch(Exception $e){} } } //now it works $div = $domd->getElementById("torrent_details"); //Print its content or error if ($div) { $dom2 = new DOMDocument(); $dom2->appendChild($dom2->importNode($div, true)); echo $dom2->saveHTML(); } else { echo "Has no element with the given ID\n"; } ?>
这两种解决方案都适用于我。
答案 2 :(得分:0)
preg_match_all('% <div \s+ id="torrent_details">(?<innerHtml>.*)</div> %six', $html, $match);
echo $match['innerHtml'];
那个可以使用,但如果页面编写得好,则只需要preg_match
而不是preg_match_all
,在给定页面上应该只有id="torrent_details"
的一个实例。
我正在收回我的答案。这将无法正常工作。使用DOM
导航文档。
答案 3 :(得分:0)
你可以这样做: /]的&GT;()LT; \ / DIV&GT; / I
哪个会给你最大的innerHTML。
答案 4 :(得分:0)
你做不到。我不会链接到这个着名的问题,因为我不喜欢顶部无意义的乱码。但是正则表达式仍然不适合匹配嵌套结构。
你可以使用一些技巧,但这既不可靠,也不一定快:
preg_match_all('#<div id="1">((<div>.*?</div>|.)*?)</div>#ims'
由于/x
标记与开头div不匹配,您的正则表达式出现问题。并且您使用了错误的断言符号。
答案 5 :(得分:0)
$ch = curl_init($scrape_address);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, '1');
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_ENCODING, "");
$data = curl_exec($ch);
$doc = new DOMDocument();
libxml_use_internal_errors(false);
$doc->strictErrorChecking = FALSE;
libxml_use_internal_errors(true);
$doc->loadHTML($data);
$xml = simplexml_import_dom($doc);
print_r($xml->body->table->tr->td->table[2]->tr->td[0]->span[0]->div);