How to get all possible image URLs from RSS feed i

2019-02-25 03:08发布

I try to use this example to get images urls from 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?

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);
                            }
}

3条回答
三岁会撩人
2楼-- · 2019-02-25 03:42

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
             }
        }                                
    }                            
}
查看更多
乱世女痞
3楼-- · 2019-02-25 03:48
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();
查看更多
▲ chillily
4楼-- · 2019-02-25 04:02

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\\~\\!\\@\\#\\$\\%\\^\\&amp;\\*\\(\\)_\\-\\=\\+\\\\\\/\\?\\.\\:\\;\\'\\,]*)?.(?:jpg|bmp|gif|png)", RegexOptions.IgnoreCase);
MatchCollection mactches = regx.Matches(text);
查看更多
登录 后发表回答