[英]How to get all possible image URLs from RSS feed item?
I try to use this example to get images urls from http://www.nydailynews.com/cmlink/NYDN.Article.rss 我尝试使用此示例从http://www.nydailynews.com/cmlink/NYDN.Article.rss获取图像网址
but no success 但没有成功
Could u help me to find all correct ways to gets all possible image URLs from RSS feed item by SyndicationItem
class? 你可以帮我找到所有正确的方法,通过
SyndicationItem
类从RSS feed项中获取所有可能的图像URL吗?
There is draft solution here but I guess should be more generic solution. 这里有草案解决方案,但我想应该是更通用的解决方案。
Thank you! 谢谢!
List<RssFeedItem> rssItems = new List<RssFeedItem>();
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<SyndicationLink>(x => x.RelationshipType == "enclosure"))
{
Uri url = enclosure.Uri;
long length = enclosure.Length;
string mediaType = enclosure.MediaType;
rssItem.ImageLinks.Add(url.AbsolutePath);
}
}
I found the solution. 我找到了解决方案。
foreach (SyndicationElementExtension extension in f.ElementExtensions)
{
XElement element = extension.GetObject<XElement>();
if (element.HasAttributes)
{
foreach (var attribute in element.Attributes())
{
string value = attribute.Value.ToLower();
if (value.StartsWith("http://") && (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("http://www.nydailynews.com/cmlink/NYDN.Article.rss");
XNamespace media = XNamespace.Get("http://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();
--EDIT-- - 编辑 -
var images = feeds.Items
.SelectMany(i => i.ElementExtensions
.Select(e => e.GetObject<XElement>().Attribute("url").Value)
)
.ToArray();
Gets a list of images from string 从字符串中获取图像列表
var text = "your text with image links";
Regex regx = new Regex("http://([\\w+?\\.\\w+])+([a-zA-Z0-9\\~\\!\\@\\#\\$\\%\\^\\&\\*\\(\\)_\\-\\=\\+\\\\\\/\\?\\.\\:\\;\\'\\,]*)?.(?:jpg|bmp|gif|png)", RegexOptions.IgnoreCase);
MatchCollection mactches = regx.Matches(text);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.