从一个URL获取div的内容

4

1
你有没有考虑过使用 Perl 和 WWW-Mechanize - cctan
5个回答

3
有许多方法可以获取url的内容:
第一种方法: http://simplehtmldom.sourceforge.net/
 Simple HTML DOM Parser

第二种方法:

第二种方法:

<?php

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

?>

第三种方法:

`You can use jquery like Selectors :` 

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


2

这是一种使用PHP进行基本操作的方法,它返回纯文本内容。但是,您可能要考虑针对您特定需求调整正则表达式。

<?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); 
?>

2

1
特别是在使用jQuery时,如果您有一个如下所示的

<div id="cool_div">Some content here</div>

你可以使用jQuery来获取
的内容,代码如下:
$('#cool_div').text(); // will return text version of contents...
$('#cool_div').html(); // will return HTML version of contents...

如果您正在使用PHP生成页面内容,那么您应该能够很好地掌握内容并在返回到屏幕并显示之前进行操作。希望这可以帮助您!

0
使用PHP,您可以尝试DOMDocument类和getElements()函数。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接