Csharp/C#教程:如何从RSS提要项中获取所有可能的图像URL?分享


如何从RSS提要项中获取所有可能的图像URL?

我尝试使用此示例从https://www.nydailynews.com/cmlink/NYDN.Article.rss获取图像url

但没有成功

你可以帮我找到所有正确的方法来从SyndicationItem类的RSS feed项中获取所有可能的图像URL吗?

这里有草案解决方案,但我想应该是更通用的解决方案。

谢谢!

  List rssItems = new List(); Stream stream = e.Result; XmlReader response = XmlReader.Create(stream); SyndicationFeed feeds = SyndicationFeed.Load(response); foreach (SyndicationItem f in feeds.Items) { RssFeedItem rssItem = new RssFeedItem(); rssItem.Description = f.Summary.Text; foreach (SyndicationLink enclosure in f.Links.Where(x => x.RelationshipType == "enclosure")) { Uri url = enclosure.Uri; long length = enclosure.Length; string mediaType = enclosure.MediaType; rssItem.ImageLinks.Add(url.AbsolutePath); } } 

我找到了解决方案。

 foreach (SyndicationElementExtension extension in f.ElementExtensions) { XElement element = extension.GetObject(); if (element.HasAttributes) { foreach (var attribute in element.Attributes()) { string value = attribute.Value.ToLower(); if (value.StartsWith("https://") && (value.EndsWith(".jpg") || value.EndsWith(".png") || value.EndsWith(".gif") )) { rssItem.ImageLinks.Add(value); // Add here the image link to some array } } } } 

 XDocument xDoc = XDocument.Load("https://www.nydailynews.com/cmlink/NYDN.Article.rss"); XNamespace media = XNamespace.Get("https://search.yahoo.com/mrss/"); var images = xDoc.Descendants(media+"content") .Where(m=>m.Attribute("type").Value=="image/jpeg") .Select(m=>m.Attribute("url").Value) .ToArray(); 

– 编辑 –

 var images = feeds.Items .SelectMany(i => i.ElementExtensions .Select(e => e.GetObject().Attribute("url").Value) ) .ToArray(); 

从字符串中获取图像列表

上述就是C#学习教程:如何从RSS提要项中获取所有可能的图像URL?分享的全部内容,如果对大家有所用处且需要了解更多关于C#学习教程,希望大家多多关注—计算机技术网(www.ctvol.com)!

 var text = "your text with image links"; Regex regx = new Regex("https://([\w+?\.\w+])+([a-zA-Z0-9\~\!\@\#\$\%\^\&\*\(\)_\-\=\+\\\/\?\.\:\;\'\,]*)?.(?:jpg|bmp|gif|png)", RegexOptions.IgnoreCase); MatchCollection mactches = regx.Matches(text); 

本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。

ctvol管理联系方式QQ:251552304

本文章地址:https://www.ctvol.com/cdevelopment/984603.html

(0)
上一篇 2021年12月20日
下一篇 2021年12月20日

精彩推荐