1

我正在尝试获取这些标签之间的数据:

<item><title>White Paper and Sales copy writer by stelladonsai</title>
<link>http://www.freelancer.com/projects/Technical-Writing-Academic-Writing/White-Paper-Sales-copy-writer.html</link>
<description>I need native English speaker to write a piece of white paper and a Sales copy.    *you must have experience.   *you must be native English speaker  *you must have sample to be attached with your cover... (Budget: &#36;30-&#36;250 USD, Jobs: Academic Writing, Technical Writing)</description>
<pubDate>Thu, 18 Oct 2012 17:23:10 -0400</pubDate>
<guid isPermaLink="false">Freelancer.com_project_2578329</guid>
</item>

我使用的 PHP 代码是:

$html = new DOMDocument();
@$html->loadHTML($txt);
$xpath = new DOMXPath( $html );

/*Begin Get Titles*/
$bquery = $xpath->query( "//item/title" );
$i=0;
foreach ($bquery as $b){
    $i=$i+1;
    $title[$i] = $b->nodeValue; 
}
/*End Get Titles*/

/*Begin Get Links*/
$bquery = $xpath->query( "//item/link" );
$i=0;
foreach ($bquery as $b){
    $i=$i+1;
    $link[$i] = $b->nodeValue;  
}
/*End Get Links*/

$txt 来自我用 CURL 抓取的一个 RSS 提要。上面的代码可以很好地获得标题,以及我想要的任何其他字段。但是当我尝试抓取并回显链接字段时,我什么也得不到。(我尝试用其他字段替换 //item/link 并且它们都有效)。

不知道我在这里做错了什么,非常感谢所有帮助。

4

1 回答 1

1

使用loadXML而不是loadHTML加载 XML 而不是 html。

http://codepad.org/0FYXiwMy

于 2012-10-19T00:13:07.170 回答