[英]How to get all possible image URLs from RSS feed item?
我嘗試使用此示例從http://www.nydailynews.com/cmlink/NYDN.Article.rss獲取圖像網址
但沒有成功
你可以幫我找到所有正確的方法,通過SyndicationItem
類從RSS feed項中獲取所有可能的圖像URL嗎?
這里有草案解決方案,但我想應該是更通用的解決方案。
謝謝!
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);
}
}
我找到了解決方案。
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();
- 編輯 -
var images = feeds.Items
.SelectMany(i => i.ElementExtensions
.Select(e => e.GetObject<XElement>().Attribute("url").Value)
)
.ToArray();
從字符串中獲取圖像列表
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.