无法提取 rss 提要项目标题
Can't extract rss feed item title
我正在使用 php 代码提取 ebay 附属公司(合作伙伴网络)rss 的标题,但没有成功。我究竟做错了什么?
对了,标题也可以link吗?
PHP
<?php
$xml = new DOMDocument();
@$xml->loadHTMLFile('http://rest.ebay.com/epn/v1/find/item.rss?keyword=%28jewelry%2Ccraft%2Cclothing%2Cshoes%2Cdiy%29&sortOrder=BestMatch&programid=1&campaignid=5337945426&toolid=10039&listingType1=All&lgeo=1&topRatedSeller=true&hideDuplicateItems=true&entriesPerPage=2&feedType=rss');
$products = array();
//Loop through each <td> tag in the dom and extract inner html
foreach($xml->getElementsByTagName('td') as $p) {
$children = $p->childNodes;
$phtml = '';
foreach ($children as $child)
{
$phtml.= $p->ownerDocument->saveHTML($child);
}
echo '<div id="mainproductafilioright1"><div class="product">' . $phtml . '</div></div>';
}
?>
会发表评论,但没有足够的代表。
该提要中没有 td 元素。它也不是 HTML 文件。
改为:
- 加载为XML
- 为 select 个标题节点创建 XPath 表达式
- 迭代返回的节点并确保它们是实际的 DOM 个节点
- 提取节点的文本值。
这是我的做法:
$doc = new DOMDocument();
$doc->loadXML("http://www.longurl.com");
$xpath = new DOMXPath($doc);
$items = $xpath->query("/rss/channel/item/title");
foreach($items as $item) {
if(XML_ELEMENT_NODE === $item->nodeType) {
echo '<div id="mainproductafilioright1"><div class="product">' . $item->textContent . '</div></div>';
}
}
你走对了。在检查提要页面时,我可以看到 td
元素在 <![CDATA[
内。但是标题在它外面,所以你不能得到标题。
尝试这个临时解决方案(这是一个全新的代码,不会与旧代码一起插入):
$feedurl = "http://rest.ebay.com/epn/v1/find/item.rss?keyword=%28jewelry%2Ccraft%2Cclothing%2Cshoes%2Cdiy%29&sortOrder=BestMatch&programid=1&campaignid=5337945426&toolid=10039&listingType1=All&lgeo=1&topRatedSeller=true&hideDuplicateItems=true&entriesPerPage=8&feedType=rss";
$rss = simplexml_load_file($feedurl);
foreach ($rss->channel->item as $item) {
$link = $item->link;
$title = $item->title;
$description = $item->description;
}
您可以使用print
输出它:
print = $description;
我正在使用 php 代码提取 ebay 附属公司(合作伙伴网络)rss 的标题,但没有成功。我究竟做错了什么? 对了,标题也可以link吗?
PHP
<?php
$xml = new DOMDocument();
@$xml->loadHTMLFile('http://rest.ebay.com/epn/v1/find/item.rss?keyword=%28jewelry%2Ccraft%2Cclothing%2Cshoes%2Cdiy%29&sortOrder=BestMatch&programid=1&campaignid=5337945426&toolid=10039&listingType1=All&lgeo=1&topRatedSeller=true&hideDuplicateItems=true&entriesPerPage=2&feedType=rss');
$products = array();
//Loop through each <td> tag in the dom and extract inner html
foreach($xml->getElementsByTagName('td') as $p) {
$children = $p->childNodes;
$phtml = '';
foreach ($children as $child)
{
$phtml.= $p->ownerDocument->saveHTML($child);
}
echo '<div id="mainproductafilioright1"><div class="product">' . $phtml . '</div></div>';
}
?>
会发表评论,但没有足够的代表。
该提要中没有 td 元素。它也不是 HTML 文件。
改为:
- 加载为XML
- 为 select 个标题节点创建 XPath 表达式
- 迭代返回的节点并确保它们是实际的 DOM 个节点
- 提取节点的文本值。
这是我的做法:
$doc = new DOMDocument();
$doc->loadXML("http://www.longurl.com");
$xpath = new DOMXPath($doc);
$items = $xpath->query("/rss/channel/item/title");
foreach($items as $item) {
if(XML_ELEMENT_NODE === $item->nodeType) {
echo '<div id="mainproductafilioright1"><div class="product">' . $item->textContent . '</div></div>';
}
}
你走对了。在检查提要页面时,我可以看到 td
元素在 <![CDATA[
内。但是标题在它外面,所以你不能得到标题。
尝试这个临时解决方案(这是一个全新的代码,不会与旧代码一起插入):
$feedurl = "http://rest.ebay.com/epn/v1/find/item.rss?keyword=%28jewelry%2Ccraft%2Cclothing%2Cshoes%2Cdiy%29&sortOrder=BestMatch&programid=1&campaignid=5337945426&toolid=10039&listingType1=All&lgeo=1&topRatedSeller=true&hideDuplicateItems=true&entriesPerPage=8&feedType=rss";
$rss = simplexml_load_file($feedurl);
foreach ($rss->channel->item as $item) {
$link = $item->link;
$title = $item->title;
$description = $item->description;
}
您可以使用print
输出它:
print = $description;