解决 在我的RSS Feeder上福彩12选5走势图超链接,缩略图和发布日期

吡x醇

新成员
已加入
2020年5月7日
留言内容
2
编程经验
Beginner
如何从以下RSS.XML Feed福彩12选5走势图超链接,缩略图和发布日期: //www.economist.com/europe/rss.xml

我正在尝试从上面的提要中获取“超链接”,“缩略图”和“发布日期”。怎么办呢?

下面是我的代码,仅福彩12选5走势图标题和描述:

C#:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Xml;
using System.ServiceModel;
using System.ServiceModel.Description;
using System.ServiceModel.Web;
using System.ServiceModel.Syndication;

namespace RSS_Demo
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                XmlReader FD_readxml = XmlReader.Create(textBox1.Text);
                SyndicationFeed FD_feed = SyndicationFeed.Load(FD_readxml);
                TabPage FD_tab = new TabPage(FD_feed.Title.Text);
                tabControl1.TabPages.Add(FD_tab);
                ListBox FD_list = new ListBox();
                FD_tab.Controls.Add(FD_list);
                FD_list.Dock = DockStyle.Fill;
                FD_list.HorizontalScrollbar = true;
                foreach (SyndicationItem FD_item in FD_feed.Items)
                {
                    FD_list.Items.Add(FD_item.Title.Text);
                    FD_list.Items.Add(FD_item.Summary.Text);
                    FD_list.Items.Add("---------");
                }
            }
            catch { }
        }
    }
}
 
由主持人最后编辑:

跳伞者

工作人员
已加入
2019年4月6日
留言内容
2,610
地点
弗吉尼亚州切萨皮克
编程经验
10+
什么's wrong with just adding more FD_list.Items? Is it because FD_Item does not contain the things you want to display?
 

羊皮

知名会员
已加入
2018年9月5日
留言内容
1,982
编程经验
10+
由于您的RSS提要没有提供帮助,因此您对此有疑问。您将需要另一种方式来浏览每个帖子,并为上载到文章的每个图像在RSS feed中抓取每个Uri的标题图像。 (请参见屏幕截图1) - That's what I done. You can then acquire each image from each post by passing the Uri's in the RSS to the Get_MetaImage(string Uri) function. The first method does all the leg work, and while the second function is called from the first method, it subsequently returns each image form each of the Uri taken from the RSS feed. And it does that by passing the Uri to the second function to request a return value.
C#:
        internal static void Request_Response()
        {
            using XmlReader reader = XmlReader.Create("//www.economist.com/europe/rss.xml");
            /* Using Linq we will go over each of the Xml items. */
            foreach ((string item_Title, string item_Discription, DateTimeOffset item_Published, Uri item_Link) in
                from SyndicationItem item in SyndicationFeed.Load(reader).Items
                let item_Title = item.Title.Text
                let item_Discription = item.Summary.Text
                let item_Published = item.PublishDate
                let item_Link = item.Links[0]
                select (item_Title, item_Discription, item_Published, item_Link.Uri))
            {
                /* When an item is selected, this section executes per each item */
                Console.WriteLine($"Title - { item_Title }");
                Console.WriteLine($"Description - { item_Discription }");
                Console.WriteLine($"Date - { item_Published }");
                Console.WriteLine($"Link - { item_Link.AbsoluteUri }");
                /* Use the Get_MetaImage method to return the image per each Uri */
                Console.WriteLine($"Thumb - { Get_MetaImage(item_Link.AbsoluteUri) }");
                /* The thumb will need resizing. This can be done in an picturebox or alike. */
            }
        }
        public static string Get_MetaImage(string Uri)
        {
            WebRequest webRequest = WebRequest.Create(Uri); /* Create the webrequest and pass in the URI passes to this method. */
            string html; string imageSubStr; string imageSubStr_Result = string.Empty;
            try
            {
                using (StreamReader reader = new StreamReader(webRequest.GetResponse().GetResponseStream()))
                {
                    /* Once we get a result, we will substring the value of the html and extract the image from the Uri source code. */
                    html = reader.ReadToEnd();
                    if (reader.EndOfStream.Equals(true))
                    {
                        imageSubStr = html.Substring(html.IndexOf("//www.economist.com/sites/default/files/images/print-edition/"));
                        imageSubStr_Result = imageSubStr.Substring(0, imageSubStr.IndexOf($"\""));
                    }
                }
            }
            catch (WebException err)
            {
                if (err.Response != null && err.Status == WebExceptionStatus.ProtocolError)
                {
                    HttpWebResponse exResponse = (HttpWebResponse)err.Response;
                    if (exResponse.StatusCode == HttpStatusCode.NotFound)
                    {
                        /* Handle what to do if no image was used or uploaded on the page */
                    }
                }
                else
                {
                    /* Handle some other error responses */
                }
            }
            return imageSubStr_Result; /* Contains image of Uri */
        }
Simple stuff really. I advise running this code on new threads, and implement some delegation for talking to your UI. I've posted a lot of examples on how to do this if you search the forums, and iterate through my posts. I also recommend you extend the code I have provided you with and include a List<Tuple<string, string, string, string, string>> and for each link capable of returning all values from the RSS and scraping method. And add them to this list, and then update your Uri from that list. You should also go over the webexception catch block to ensure I haven't missed anything. Screenshot 2 shows a working example. Hope this helps, and hopefully you understand how i explained it, as I am rushing out at the minute.

屏幕截图1: 截屏
屏幕截图2: 截屏
 

吡x醇

新成员
已加入
2020年5月7日
留言内容
2
编程经验
Beginner
What's wrong with just adding more FD_list.Items? Is it because FD_Item does not contain the things you want to display?
你好,

谢谢 @跳伞,说实话,我只是一个新手。正如我刚刚发现的那样 @Sheepings 缩略图不会福彩12选5走势图在我的RSS.XML提要中,从这里我要说的是,我只想福彩12选5走势图从RSS.XML提要中提供的“链接”和“发布”日期,但不能福彩12选5走势图为有足够的信心使用“ FD_Item”命令来检索此类内容。您能在这方面帮忙吗?

亲切的问候,
吡x醇
由于您的RSS提要没有提供帮助,因此您对此有疑问。您将需要另一种方式来浏览每个帖子,并为上载到文章的每个图像在RSS feed中抓取每个Uri的标题图像。 (请参见屏幕截图1) - That's what I done. You can then acquire each image from each post by passing the Uri's in the RSS to the Get_MetaImage(string Uri) function. The first method does all the leg work, and while the second function is called from the first method, it subsequently returns each image form each of the Uri taken from the RSS feed. And it does that by passing the Uri to the second function to request a return value.
C#:
        internal static void Request_Response()
        {
            using XmlReader reader = XmlReader.Create("//www.economist.com/europe/rss.xml");
            /* Using Linq we will go over each of the Xml items. */
            foreach ((string item_Title, string item_Discription, DateTimeOffset item_Published, Uri item_Link) in
                from SyndicationItem item in SyndicationFeed.Load(reader).Items
                let item_Title = item.Title.Text
                let item_Discription = item.Summary.Text
                let item_Published = item.PublishDate
                let item_Link = item.Links[0]
                select (item_Title, item_Discription, item_Published, item_Link.Uri))
            {
                /* When an item is selected, this section executes per each item */
                Console.WriteLine($"Title - { item_Title }");
                Console.WriteLine($"Description - { item_Discription }");
                Console.WriteLine($"Date - { item_Published }");
                Console.WriteLine($"Link - { item_Link.AbsoluteUri }");
                /* Use the Get_MetaImage method to return the image per each Uri */
                Console.WriteLine($"Thumb - { Get_MetaImage(item_Link.AbsoluteUri) }");
                /* The thumb will need resizing. This can be done in an picturebox or alike. */
            }
        }
        public static string Get_MetaImage(string Uri)
        {
            WebRequest webRequest = WebRequest.Create(Uri); /* Create the webrequest and pass in the URI passes to this method. */
            string html; string imageSubStr; string imageSubStr_Result = string.Empty;
            try
            {
                using (StreamReader reader = new StreamReader(webRequest.GetResponse().GetResponseStream()))
                {
                    /* Once we get a result, we will substring the value of the html and extract the image from the Uri source code. */
                    html = reader.ReadToEnd();
                    if (reader.EndOfStream.Equals(true))
                    {
                        imageSubStr = html.Substring(html.IndexOf("//www.economist.com/sites/default/files/images/print-edition/"));
                        imageSubStr_Result = imageSubStr.Substring(0, imageSubStr.IndexOf($"\""));
                    }
                }
            }
            catch (WebException err)
            {
                if (err.Response != null && err.Status == WebExceptionStatus.ProtocolError)
                {
                    HttpWebResponse exResponse = (HttpWebResponse)err.Response;
                    if (exResponse.StatusCode == HttpStatusCode.NotFound)
                    {
                        /* Handle what to do if no image was used or uploaded on the page */
                    }
                }
                else
                {
                    /* Handle some other error responses */
                }
            }
            return imageSubStr_Result; /* Contains image of Uri */
        }
Simple stuff really. I advise running this code on new threads, and implement some delegation for talking to your UI. I've posted a lot of examples on how to do this if you search the forums, and iterate through my posts. I also recommend you extend the code I have provided you with and include a List<Tuple<string, string, string, string, string>> and for each link capable of returning all values from the RSS and scraping method. And add them to this list, and then update your Uri from that list. You should also go over the webexception catch block to ensure I haven't missed anything. Screenshot 2 shows a working example. Hope this helps, and hopefully you understand how i explained it, as I am rushing out at the minute.

屏幕截图1: 截屏
屏幕截图2: 截屏

谢谢 @Sheepings 我确实感谢您所付出的努力,尽管老实说,我对编程只是陌生的,到目前为止仅了解基本概念/局限性。因此,我只想福彩12选5走势图RSS.XML feed中的“链接”和“发布日期”。你能帮忙吗?

亲切的问候,
吡x醇
 

跳伞者

工作人员
已加入
2019年4月6日
留言内容
2,610
地点
弗吉尼亚州切萨皮克
编程经验
10+
如果您花时间深入了解正在使用的各种对象/类的文档,则将极大地帮助您的学习。仅仅模仿在线或教程中找到的代码,只会使您到目前为止。

要获取发布日期,只需访问 PublishDate属性.

联合项目可能具有许多链接。见 链接属性. If you want the first one, you can use the First() LINQ extension method, or use the [0] indexing property of the Collection<SyndicationLink>. (The latter is more commonly used in the sample code written back in the early 2010's because LINQ was still in its infancy and had tenuous adoption.)
 

羊皮

知名会员
已加入
2018年9月5日
留言内容
1,982
编程经验
10+
我只想福彩12选5走势图“链接”和“发布”日期
您只需要删除该功能和其他几行。试试这个 :
C#:
        internal static void Execute_Response()
        {
            using XmlReader reader = XmlReader.Create("//www.economist.com/europe/rss.xml");
            /* Using Linq we will go over each of the Xml items. */
            foreach ((DateTimeOffset item_Published, Uri item_Link) in
                from SyndicationItem item in SyndicationFeed.Load(reader).Items
                let item_Published = item.PublishDate
                let item_Link = item.Links[0]
                select (item_Published, item_Link.Uri))
            {
                /* When an item is selected, this section executes per each item */
                Console.WriteLine($"Date - { item_Published }");
                Console.WriteLine($"Link - { item_Link.AbsoluteUri }");
            }
        }
希望能帮助到你。
 
最佳 底部