[英]How can i extract the date and time from the html page file?
在課堂上我有:
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
土都土耳其
因此,我需要獲取的鏈接列表是根據所有國家/地區代碼以及每個地區(計數)的所有日期和時間構建的,以便以后可以下載圖像。
因此,在列表中,例如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
當然,這取決於從sat24頁面提取的每個地區/國家的日期和時間。
然后在完成創建列表以下載圖像后,例如每個鏈接: http : //www.sat24.com/image2.ashx? region=is&time=201612271910&ir=true應該下載並保存為圖像。
這是一個使用敏捷包從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小提琴,表明它可以正常工作
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.