gpt4 book ai didi

PHP 简单 HTML DOM 解析器 - RSS 中的链接元素

转载 作者:可可西里 更新时间:2023-11-01 01:01:20 24 4
gpt4 key购买 nike

我刚开始使用 PHP Simple HTML DOM Parser (http://simplehtmldom.sourceforge.net/),在解析 XML 时遇到了一些问题。

我可以完美地解析来自 HTML 文档的所有链接,但无法解析来自 RSS 提要(XML 格式)的链接。例如,我想解析来自 http://www.bing.com/search?q=ipod&count=50&first=0&format=rss 的所有链接。所以我使用这段代码:

$content = file_get_html('http://www.bing.com/search?q=ipod&count=50&first=0&format=rss');

foreach($content->find('item') as $entry)
{
$item['title'] = $entry->find('title', 0)->plaintext;
$item['description'] = $entry->find('description', 0)->plaintext;
$item['link'] = $entry->find('link', 0)->plaintext;
$parsed_results_array[] = $item;
}

print_r($parsed_results_array);

脚本解析标题和描述,但链接元素为空。有任何想法吗?我的猜测是“链接”是保留字之类的,那么我该如何让解析器工作呢?

最佳答案

我建议您使用正确的工具来完成这项工作。使用 SimpleXML:另外,它是内置的 :)

$xml = simplexml_load_file('http://www.bing.com/search?q=ipod&count=50&first=0&format=rss');
$parsed_results_array = array();
foreach($xml as $entry) {
foreach($entry->item as $item) {
// $parsed_results_array[] = json_decode(json_encode($item), true);
$items['title'] = (string) $item->title;
$items['description'] = (string) $item->description;
$items['link'] = (string) $item->link;
$parsed_results_array[] = $items;
}
}

echo '<pre>';
print_r($parsed_results_array);

应该产生如下内容:

Array
(
[0] => Array
(
[title] => Apple - iPod
[description] => Learn about iPod, Apple TV, and more. Download iTunes for free and purchase iTunes Gift Cards. Check out the most popular TV shows, movies, and music.
[link] => http://www.apple.com/ipod/
)

[1] => Array
(
[title] => iPod - Wikipedia, the free encyclopedia
[description] => The iPod is a line of portable media players designed and marketed by Apple Inc. The first line was released on October 23, 2001, about 8½ months after ...
[link] => http://en.wikipedia.org/wiki/IPod
)

关于PHP 简单 HTML DOM 解析器 - RSS 中的链接元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24889659/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com