【发布时间】:2009-12-28 20:24:49
【问题描述】:
我如何扫描一个 html 页面,以获取某个 div 中的文本?
【问题讨论】:
标签: php html html-content-extraction
我如何扫描一个 html 页面,以获取某个 div 中的文本?
【问题讨论】:
标签: php html html-content-extraction
最简单的方法是使用Simple HTML DOM parser
// Create a DOM object from a URL
$html = file_get_html('http://www.google.com/');
// Find all <div> which attribute id=foo
$ret = $html->find('div[id=foo]');
【讨论】:
您可以按照其他人的建议使用内置功能,或者您可以尝试将 Simple HTML DOM Parser 实现为一个简单的 PHP 类和一些辅助函数。它支持 CSS 选择器样式的屏幕抓取(例如在 jQuery 中),可以处理无效的 HTML,甚至提供熟悉的界面来操作 DOM。
【讨论】:
preg_match() 匹配你想要的子字符串或使用 dom/xml。
【讨论】: