gpt4 book ai didi

javascript - 使用 React Native 解析 RSS feed 时,如何获取描述标签内的描述和 imageUrl?

转载 作者:行者123 更新时间:2023-11-28 03:21:32 24 4
gpt4 key购买 nike

这是我尝试解析的 RSS 提要中的项目的样子:

<item>
<title>
Former cop gets 18 years in prison for shooting civilian to death
</title>
<description>
<![CDATA[
<a href="https://e.vnexpress.net/news/news/former-cop-gets-18-years-in-prison-for-shooting-civilian-to-death-4020110.html"><img width=130 height=100 src="https://i-english.vnecdn.net/2019/11/30/phuoc146331574997301-157508140-8364-4799-1575086150_180x108.jpg" ></a></br>A Dong Nai court sentenced a former traffic policeman to 18 years in prison on Friday for fatally shooting a man in a family feud.
]]>
</description>
<pubDate>Sat, 30 Nov 2019 12:00:00 +0700</pubDate>
<link>
https://e.vnexpress.net/news/news/former-cop-gets-18-years-in-prison-for-shooting-civilian-to-death-4020110.html
</link>
<guid>
https://e.vnexpress.net/news/news/former-cop-gets-18-years-in-prison-for-shooting-civilian-to-death-4020110.html
</guid>
<slash:comments>0</slash:comments>
</item>

这就是我从 RSS feed 获取 title 的方法,我使用的是 react-native-rss-parser ( https://www.npmjs.com/package/react-native-rss-parser ):

fetch('https://vnexpress.net/rss/tin-moi-nhat.rss')
.then((response) => response.text())
.then((responseData) => rssParser.parse(responseData))
.then((rss) => {
this.setState(prevState => ({
...prevState,

title0: rss.items[0] ? rss.items[0].title : '',
caption0: rss.items[0] ? rss.items[0].description : ''
}))
})
const Feeds = ([
{
pic: require('../assets/images/image.jpg'),
title: this.state.title0,
caption: this.state.caption0
},
])

我正在尝试解析嵌套在描述标记内的descriptionimageUrl。我该怎么做呢?有什么建议吗?

最佳答案

使用jquery,你可以解析它并获取里面的所有字符串,如果你只需要最后一个字符串,你可以获取nodeNames列表中的最后一个索引

    str = '<a href="https://e.vnexpress.net/news/news/former-cop-gets-18-years-in-prison-for-shooting-civilian-to-death-4020110.html"><img width=130 height=100 src="https://i-english.vnecdn.net/2019/11/30/phuoc146331574997301-157508140-8364-4799-1575086150_180x108.jpg" ></a></br>A Dong Nai court sentenced a former traffic policeman to 18 years in prison on Friday for fatally shooting a man in a family feud.',
html = $.parseHTML( str ),
nodeNames = [];

// Gather the parsed HTML's node names
$.each( html, function( i, el ) {
if(el.nodeName=='#text'){
nodeNames[ i ] = el.nodeValue;
}else{
nodeNames[ i ] = el.innerText;
}
});
console.log(nodeNames);
<小时/>

仅使用 js 将返回文本内容

function parseHTML(html) {
var t = document.createElement('template');
t.innerHTML = html;
return t.content.cloneNode(true).textContent;
}

关于javascript - 使用 React Native 解析 RSS feed 时,如何获取描述标签内的描述和 imageUrl?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59114033/

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