通过 PHP 提取网页内容


extracting webpage content through php

可能的重复项:
如何使用PHP解析和处理HTML?

<body> 
    <table align="center">
<?
 $ip=$_SERVER['REMOTE_ADDR'];
 $url=file_get_contents("http://whatismyipaddress.com/ip/$ip");
 preg_match_all('/<th>(.*?)<'/th><td>(.*?)<'/td>/s',$url,$output,PREG_SET_ORDER);
 for ($q=0; $q < 25; $q++) {
    if ($output[$q][1]) {
        if (!stripos($output[$q][2],"Blacklist")) {
            echo "<tr><td>".$output[$q][1]."</td><td>".$output[$q][2]."</td></tr>";
        }
    }
}
?> 
    </table>
</body> 

现在,通过这段代码,我得到了很多信息,如isp,国家等。

如何提取内容,以便将它们加载到我的数据库中,如$country、$isp等?我认为如果将其转换为xml可以完成,但我不确定。

你正在寻找PHP简单的HTML DOM解析器

使用它,您可以:

$html = file_get_html('http://www.google.com/');

然后使用解析器中定义的函数(如查找(从HTML中获取单个标签。

阅读此内容