du0531 2012-06-05 15:41
浏览 48
已采纳

抓取HTML表格数据并创建XML或JSON文档

I need to scrape some website data from a table on a website and create an XML or JSON document that will be used by an app. and i have some problem getting the below data.

The table looks like this:

<table border="0" cellpadding="3" cellspacing="0" bgcolor="#DDEEFF" width="100%">
<tr>
<td width="20%" ><font face="Verdana, Arial" size="1">SRC</a></td></font>
    <td width="58%" ><font face="Verdana, Arial" size="1"><a href="http://example.com/this/news?id=1&by=today" onMouseOver="a('Open Bulletin');return true" onMouseOut="b()">Welcome</font></a></td>
<td width="17%" align="center"><font face="Verdana, Arial" size="1">Event</td></font>
    </tr>   

<tr>
<td width="20%" ><font face="Verdana, Arial" size="1">FMD</a></td></font>
    <td width="58%" ><font face="Verdana, Arial" size="1"><a href="http://example.com/this/news?id=2&by=today" onMouseOver="a('Open Bulletin');return true" onMouseOut="b()">Another News</font></a></td>
<td width="17%" align="center"><font face="Verdana, Arial" size="1">Updates</td></font>
    </tr>   
</td>

And I would like to create an XML feed or JSON that looks like this:

<bulletins>
    <title>Welcome</title>
    <id>1</id>
    <type>News</type>
</bulletins>

<bulletins>
    <title>Another News</title>
    <id>2</id>
    <type>Updates</type>
</bulletins>

Here is my current code :

<?php
$body = explode('<table border="0" cellpadding="3" cellspacing="0" bgcolor="#DDEEFF" width="100%">', $html);

$xml = simplexml_load_string("<?xml version='1.0' encoding='utf-8'?><xml />");

$rows = array();
foreach (array_slice(explode('<tr>', end($body)), 1) as $row)
{   
    preg_match('#<a.*?href="(.*?)".*?>(.*?)</a>#i', $row, $title);
    preg_match('/<a.*href="(.*)".*>(.*)<\/a>/iU', $row, $id);
   // preg_match('/type">([^<]+)<\/td>/', $row, $type); 

    $node = $xml->addChild('bulletins');

    $node->addChild('title', $title[1]);
    $node->addChild('id', $id[1]);
   // $node->addChild('type', $due[1]);
}

header('Content-Type: text/xml');
echo $xml->asXML();
?>

But the problem i got this

<xml>
    <bulletins>
        <title>http://example.com/this/news?id=1</title>
        <id>http://example.com/this/news?id=1</id>
    </bulletins>
    <bulletins>
        <title>http://example.com/this/news?id=2</title>
        <id>http://example.com/this/news?id=2</id>
    </bulletins>
</xml>
  • 写回答

3条回答 默认 最新

  • doudun2212 2012-06-06 00:40
    关注

    Here's a quick example to get you started using only dom functions:

    $dom = new DOMDocument();
    @$dom->loadHTMLFile(url);
    $xpath = new DOMXPath($dom);
    
    $xml = new DOMDocument();
    foreach($xpath->query('//table/tr') as $tr) {
      $bulletin = $xml->appendChild($xml->createElement("bulletin"));
      $title = $xpath->query('.//td[2]//a', $tr)->item(0)->nodeValue;
      $bulletin->appendChild($xml->createElement("title",$title));
      $type = $xpath->query('.//td[3]/font', $tr)->item(0)->nodeValue;
      $bulletin->appendChild($xml->createElement("type",$type));
    }
    echo $xml->saveXML();
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥200 uniapp长期运行卡死问题解决
  • ¥15 latex怎么处理论文引理引用参考文献
  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?
  • ¥15 乘性高斯噪声在深度学习网络中的应用