0 php
我最近一直在尝试从给定的 html 文件中提取列表信息,
例如,我有一个 html 页面,其中包含许多公司的列表,包括他们的电话号码、地址等'
每家公司都有自己的桌子,每张桌子都是这样开始的: <table border="0">
我尝试使用 PHP 获取所有信息,然后再使用它,例如将其放入 txt 文件中,或者只是导入到数据库中。
我认为实现目标的方法是使用正则表达式,这是我在 php 中真正遇到问题的事情之一,
如果你们能在这里帮助我,我将不胜感激。(我只需要知道要寻找什么,或者至少可以帮助我一点的东西,而不是完整的代码或类似的东西)
提前致谢!!
我建议查看 PHPDOMDocument并使用实际的 HTML 解析器而不是正则表达式来解析文件。
有一些非常直接的获取表格的GetElementsByTagName方法,例如方法。
<?php
$htmlCode = /* html code here */
// create a new HTML parser
// http://php.net/manual/en/class.domdocument.php
$dom = new DOMDocument();
// Load the HTML in to the parser
// http://www.php.net/manual/en/domdocument.loadhtml.php
$dom->LoadHTML($htmlCode);
// Locate all the tables within the document
// http://www.php.net/manual/en/domdocument.getelementsbytagname.php
$tables = $dom->GetElementsByTagName('table');
// iterate over all the tables
$t = 0;
while ($table = $tables->item($t++))
{
// you can now work with $table and find children within, check for
// specific classes applied--look for anything that would flag this
// as the type of table you'd like to parse and work with--then begin
// grabbing information from within it and treating it as a DOMElement
// http://www.php.net/manual/en/class.domelement.php
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1065 次 |
| 最近记录: |