Get contents of a div from a URL

Possible Duplicate:
How to implement a web scraper in PHP?
How to parse and process HTML with PHP?

I need to crawl through a page and get the contents of a particular div. I have php and javascript as my two main options. How can it be done?


This is quite a basic method to do it PHP and it returns the content in plain text. However you might consider revising the regex for your particular need.

<?php
  $link = file_get_contents("http://www.domain.com");
  $file = strip_tags($link, "<div>");
  preg_match_all("/<div/>(?:[^<]*)</div>/is", $file, $content);
  print_r($content); 
?>

There are many ways to get the contents of an url:

First Method:

http://simplehtmldom.sourceforge.net/

 Simple HTML DOM Parser

Second Method :

<?php

  $contents = file_get_contents("http://www.url.com");
  $contents = strip_tags($contents, "<div>");
  preg_match_all("/<div/>(?:[^<]*)</div>/is", $contents, $file_contents);

?>

Third Method:

`You can use jquery like Selectors :` 

http://api.jquery.com/category/selectors/


您可以使用SimpleDomParser,如http://simplehtmldom.sourceforge.net/manual.htm中所述,但它需要PHP5 +,不过好处是,您可以通过选择器(如jQuery)在HTML页面上找到标签。

链接地址: http://www.djcxy.com/p/29904.html

上一篇: 将XML数据提取到php

下一篇: 从URL获取div的内容