2016-12-27 194 views
-1

在課堂上,我有:如何從HTML頁面文件中提取日期和時間?

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 
using System.IO; 
using System.Net; 

namespace SatelliteImages 
{ 
    class ExtractImages 
    { 
     static WebClient client; 
     List<string> imagesUrls = new List<string>(); 
     static string htmltoextract; 
     static string link; 
     static string text; 
     public static List<string> countriescodes = new List<string>(); 
     public static List<string> countriesnames = new List<string>(); 
     private static int lastsatimage = 0; 
     private static string Next_Sat_File; 
     private static string temp_sat_dir; 

     public void Init() 
     { 
      ExtractCountires(); 
     } 

     public static void ExtractCountires() 
     { 
      try 
      { 
       htmltoextract = "http://sat24.com/en/?ir=true"; 
       client = new WebClient(); 
       client.DownloadFile(htmltoextract, @"c:\temp\sat24.html"); 
       client.Dispose(); 

       string tag1 = "<li><a href=\"/en/"; 
       string tag2 = "</a></li>"; 

       string s = System.IO.File.ReadAllText(@"c:\temp\sat24.html"); 
       s = s.Substring(s.IndexOf(tag1)); 
       s = s.Substring(0, s.LastIndexOf(tag2) + tag2.ToCharArray().Length); 
       s = s.Replace("\r", "").Replace("\n", "").Replace(" ", ""); 

       string[] parts = s.Split(new string[] { tag1, tag2 }, StringSplitOptions.RemoveEmptyEntries); 


       string tag3 = "<li><ahref=\"/en/"; 

       for (int i = 0; i < parts.Length; i++) 
       { 
        if (i == 17) 
        { 
         break; 
        } 
        string l = ""; 
        if (parts[i].Contains(tag3)) 
         l = parts[i].Replace(tag3, ""); 

        string z1 = l.Substring(0, l.IndexOf('"')); 
        countriescodes.Add(z1); 

        string z2 = parts[i].Substring(parts[i].LastIndexOf('>') + 1); 
        countriesnames.Add(z2); 
       } 
      } 
      catch (Exception e) 
      { 

      } 
     } 
    } 
} 

我發現,在該文件中讀取sat24.html文件時的日期和時間在這個部分中的HTML文件:

var arrayImageTimes = []; 
arrayImageTimes.push('201612271810');arrayImageTimes.push('201612271825');arrayImageTimes.push('201612271840');arrayImageTimes.push('201612271855');arrayImageTimes.push('201612271910');arrayImageTimes.push('201612271925');arrayImageTimes.push('201612271940');arrayImageTimes.push('201612271955');arrayImageTimes.push('201612272010');arrayImageTimes.push('201612272025'); 

那麼什麼我想要做的是提取日期和時間,並將它們添加到兩個列表:201612271810因此,第一個列表將是這種格式。

第二個名單是日期和時間,不知道是什麼格式,但是:年=2016個月= 12天=27小時=18分鐘= 10

什麼,我想以後做的是建立一個新的鏈接對於每個日期和時間新的鏈接應該是這種格式:如果我們採取例如日期和時間201612271810.然後:http://www.sat24.com/image2.ashx?region=is&time=201612271810&ir=true問題是我如何提取日期和時間,然後使用我的ExtractCountries方法來建立鏈接。 在ExtractCountries方法中,我得到兩個列表中的一個國家代碼和一個國家名稱。

我需要建立鏈接的是國家/地區代碼和國家/地區的日期和時間。

於是我就可以使用代碼的列表,並附有日期時間,例如:

第一個列表會的日期和時間,因爲他們是在HTML文件格式:比如第一次約會和時間:

http://www.sat24.com/image2.ashx?region=is&time=201612271810&ir=true

哪裏=以色列和201612271810的日期時間該圖片鏈接。 或者例如

http://www.sat24.com/image2.ashx?region=tu&time=201612271810&ir=true 

地區TU是土耳其

所以我需要的是得到的來自所有國家的代碼和每個區域(countrey)所有日期和時間建立鏈接列表,以便我可以在以後下載圖片。因此,在列表例如URI字符串類型或字符串列表類型

將在第一10個指數法是這樣的:

http://www.sat24.com/image2.ashx?region=tu&time=201612271825&ir=true 
http://www.sat24.com/image2.ashx?region=tu&time=201612271840&ir=true 
http://www.sat24.com/image2.ashx?region=tu&time=201612271855&ir=true 
http://www.sat24.com/image2.ashx?region=tu&time=201612271910&ir=true 
. 
. 
. 
. 
. 
. 
http://www.sat24.com/image2.ashx?region=is&time=201612271810&ir=true 
http://www.sat24.com/image2.ashx?region=is&time=201612271825&ir=true 
http://www.sat24.com/image2.ashx?region=is&time=201612271840&ir=true 
http://www.sat24.com/image2.ashx?region=is&time=201612271910&ir=true 

Ofcourse dpending從sat24頁面每個區域/國家提取的日期和時間。

然後當完成創建列表下載圖像。例如每個鏈接:http://www.sat24.com/image2.ashx?region=is&time=201612271910&ir=true應該被下載並保存爲圖像。

+3

您應該使用HTML敏捷性包。 – SLaks

回答

3

這是一個使用敏捷包從html文檔中提取信息的示例。

using System; 
using System.Linq; 
using System.IO; 
using System.Xml; 
using System.Net; 
using HtmlAgilityPack; 

public class Program 
{ 
    public static void Main() 
    { 
     var wc = new WebClient(); 
     wc.BaseAddress = "http://sat24.com/"; 
     HtmlDocument doc = new HtmlDocument(); 

     var temp = wc.DownloadData("/en"); 
     doc.Load(new MemoryStream(temp));  

     var secTokenScript = doc.DocumentNode.Descendants() 
      .Where(e => 
        String.Compare(e.Name, "script", true) == 0 && 
        String.Compare(e.ParentNode.Name, "div", true) == 0 && 
        e.InnerText.Length > 0 && 
        e.InnerText.Trim().StartsWith("var region") 
       ).FirstOrDefault().InnerText; 
     var securityToken = secTokenScript; 
     securityToken = securityToken.Substring(0, securityToken.IndexOf("arrayImageTimes.push")); 
     securityToken = secTokenScript.Substring(securityToken.Length).Replace("arrayImageTimes.push('", "").Replace("')", ""); 
     var dates = securityToken.Trim().Split(new string[] { ";"}, StringSplitOptions.RemoveEmptyEntries); 
     var scriptDates = dates.Select(x => new ScriptDate { DateString = x }); 
     foreach(var date in scriptDates) 
     { 
      Console.WriteLine("Date String: '" + date.DateString + "'\tYear: '" + date.Year + "'\t Month: '" + date.Month + "'\t Day: '" + date.Day + "'\t Hours: '" + date.Hours + "'\t Minutes: '" + date.Minutes + "'"); 
     } 

    } 


    public class ScriptDate 
    { 
     public string DateString {get;set;} 
     public int Year 
     { 
      get 
      { 
       return Convert.ToInt32(this.DateString.Substring(0, 4)); 
      } 
     } 
     public int Month 
     { 
      get 
      { 
       return Convert.ToInt32(this.DateString.Substring(4, 2)); 
      } 
     } 
     public int Day 
     { 
      get 
      { 
       return Convert.ToInt32(this.DateString.Substring(6, 2)); 
      } 
     } 
     public int Hours 
     { 
      get 
      { 
       return Convert.ToInt32(this.DateString.Substring(8, 2)); 
      } 
     } 
     public int Minutes 
     { 
      get 
      { 
       return Convert.ToInt32(this.DateString.Substring(10, 2)); 
      } 
     } 




    } 


} 

我還創建了一個.Net Fiddle顯示,這個工程

相關問題