//下载页面源文件并将其转换成UTF8编码格式的STRING
//string mainData = Encoding.UTF8.GetString(wc.DownloadData(string.Format("你要采集的网页地址")));
string path = Server.MapPath("data");
FileStream fs = new FileStream(path + "\\data.txt", FileMode.OpenOrCreate);
//下载网页源文件到本地
wc.DownloadFile("http://hotels.ctrip.com/citylist", "path");
//读取下载下来的源文件HTML格式的字符串
string mainData = File.ReadAllText("path", Encoding.UTF8);
var cityList = new List<City>();//定义泛型列表存放城市名称及对应的酒店URL
var links = Regex.Matches(mainData, @"<a[^>]+href=""*(?<href>/hotel/[^>\s]+)""\s*[^>]*>(?<text>(?!.*img).*?)</a>", RegexOptions.IgnoreCase);
StreamWriter sw = new StreamWriter(fs);
int i = 0;
foreach (Match match in links)
{
//string name = match.Groups["text"].Value;
//string url = "http://hotels.ctrip.com" + match.Groups["href"].Value;
//sw.WriteLine(name +" "+ url);
i++;
var city = new City
{
CityName = i + match.Groups["text"].Value,
Uri = new Uri("http://hotels.ctrip.com" + match.Groups["href"].Value
)
};
if (!cityList.Contains(city)) cityList.Add(city);//将数据加入到泛型列表
//Console.WriteLine(city.CityName + "|" + city.Uri);//将城市名称及URL显示到控制台
sw.WriteLine(city.CityName + " | " + city.Uri);
}
mainData = mainData.Substring(mainData.IndexOf("<a class=\"titlelnk\" href=\"") + 26);
//获取文章页面的链接地址
// string articleAddr = mainData.Substring(0, mainData.IndexOf("\""));
//获取文章标题
// string articleTitle = mainData.Substring(mainData.IndexOf("target=\"_blank\">") + 16,
// mainData.IndexOf("</a>") - mainData.IndexOf("target=\"_blank\">") - 16);
sw.Close();
标签:city,string,c#,汉字,爬取,IndexOf,mainData,new,match From: https://www.cnblogs.com/jigeqiu/p/18069982