繁体   English   中英

如何从网页中提取特定div的内容?

[英]How can I extract content of a specific div from a webpage?

我想从网页class='box'使用class='box'加载特定div的内容,为此我使用了简单HTML DOM。 但我无法为preg_match编写清晰的模式,这是我的php代码:

<?php
   $url = "http://www.example.com/pages/";
   $page_all = file_get_contents($url); 

   preg_match(...?);


   echo "<pre>";
   print_r($div_array[0]);
   echo "</pre>";
?>

请帮助我为preg_match编写正确的模式

SimpleHtmlDOM:

$html = new simple_html_dom();

// Load from a string
$html->load('<html><body><p>Hello World!</p><p>We're here</p></body></html>');

// Load a file
$html->load_file('http://net.tutsplus.com/');

# get an element representing the second paragraph  
$element = $html->find("div[class=box1]");

#access HTML attr
$element->innertext .= "Somthing";

#save and echo
echo $element->save();

您应该签出: http : //simplehtmldom.sourceforge.net/

一个例子是:

$html = new simple_html_dom();

$html = file_get_html('http://www.example.com/pages/');

$ret = $html->find('div[class=box]');

不要在Regex上浪费时间,有很多工具可以完成这项工作。

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM