本问题已经有最佳答案,请猛点这里访问。
Possible Duplicate:
How to implement a web scraper in PHP?
How to parse and process HTML with PHP?
我需要爬过一个页面并获取特定分区的内容。我有两个主要选项:PHP和JavaScript。怎么做?
我知道,使用regex!
你有没有想过Perl和WWW机械化?
有很多方法可以获取URL的内容:
第一种方法:
http://simplehtmldom.sourceforge.net网站/
Simple HTML DOM Parser
第二种方法:
$contents = file_get_contents("http://www.url.com");
$contents = strip_tags($contents,"");
preg_match_all("/(?:[^/is", $contents, $file_contents);
?>
第三方法:
`You can use jquery like Selectors :`
http://api.jquery.com/category/selectors/
您可以使用SimpleDomParser,如本文所述http://simplehtmldom.sourceforge.net/manual.htm不过,它需要php5+,但好的是您可以在HTML页面上找到带有选择器的标记,就像jquery一样。
这是一个非常基本的PHP方法,它以纯文本形式返回内容。但是,您可能会考虑根据您的特殊需要修改regex。
$link = file_get_contents("http://www.domain.com");
$file = strip_tags($link,"");
preg_match_all("/(?:[^/is", $file, $content);
print_r($content);
?>
特别是对于jquery,如果您有一个div,如下所示:
Some content here
您可以使用jquery获取div的内容,如下所示:
$('#cool_div').text(); // will return text version of contents...
$('#cool_div').html(); // will return HTML version of contents...
如果您使用PHP来生成页面的内容,那么您应该能够对内容进行适当的处理,并在内容返回屏幕并显示之前对其进行操作。希望这有帮助!
使用php,可以尝试domDocument类和getElements()函数