-1

这是代码:

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.Net;
using System.Text.RegularExpressions;
using System.IO;

namespace DownloadImages
{
    public partial class Form1 : Form
    {
        string f;

        public Form1()
        {
            InitializeComponent();

            string localFilename = @"d:\localpath\";

                using (WebClient client = new WebClient())
                {
                    client.DownloadFile("http://www.sat24.com/foreloop.aspx?type=1&continent=europa#",localFilename + "test.html");
                }

                f = File.ReadAllText(localFilename + "test.html");
                test();
        }

        private void test()
        {
            List<string> imagesUrls = new List<string>();
            int startIndex = 0;
            int endIndex = 0;
            int position = 0;

            string startTag = "http://www.niederschlagsradar.de/images.aspx";
            string endTag = "cultuur=en-GB&continent=europa";    
            startIndex = f.IndexOf(startTag);

            while (startIndex > 0)
            {

                endIndex = f.IndexOf(endTag,startIndex);
                if (endIndex == -1)
                {
                    break;
                }
                string t = f.Substring(startIndex, endIndex - startIndex + endTag.Length);
                imagesUrls.Add(t);    
                position = endIndex + endTag.Length;    
                startIndex = f.IndexOf(startTag,position);
            }
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }
    }
}

最后的 List 包含 63 个索引。例如,第一个索引 0 包含:

http://www.niederschlagsradar.de/images.aspx?jaar=-6&type=europa.precip&datum=201309151800&cultuur=en-GB&continent=europa

例如,索引 5 包含:

http://www.niederschlagsradar.de/images.aspx?jaar=-6&type=europa.precip&datum=201309160600&cultuur=en-GB&continent=europa

最后一个索引是问题所在,它包含我想要在其他索引中的字符串,但它还包含来自最后一个索引的其余文件内容:

这是最后一个索引的一部分:

http://www.niederschlagsradar.de/images.aspx?jaar=-6&type=europa.precip&cultuur=thumbnail&continent=europa" border="0"/></a></li><li style="margin-top: -12px;text-align: center;"><a href="/?ir=true&co=true&li=false" target="_top" class="white"><div 

但最后一个索引应该只有:

http://www.niederschlagsradar.de/images.aspx?jaar=-6&type=europa.precip&cultuur=thumbnail&continent=europa

我该如何解决?

4

2 回答 2

0

您可能想使用这个很酷的工具:

HtmlAgilityPack

一些信息:

于 2013-09-15T19:30:44.947 回答
0

对于抓取网页,htmlagilitypack 非常有用。这是一个有用的解释示例: http ://codingfields.com/guides/htmlagilitypack/

这个页面解释了几个关键: http: //www.codeproblem.com/articles/languages/81-net-framework/74-html-parsing-in-c-using-html-agility-pack

这个页面也不错: http ://beletsky.net/2010/09/crawling-web-sites-with-htmlagilitypack.html

此示例说明了替换上下文:http ://sparkingnaz.wordpress.com/2013/03/12/how-to-use-html-agility-pack-parsing-html-documents-with-the-html-agility-pack-提取内容节点并替换内容/

于 2013-09-15T19:31:10.513 回答