class Program
{
static void Main(string[] args)
{
var wc = new WebClient();
var html = wc.DownloadString("http://zhidao.baidu.com/question/499087825.html?seed=0");
var regex = new Regex("<span class=\"question-title\" data-accusearea=\"qTitle\">(?<title>.*?)</span>");
if (regex.IsMatch(html))
{
var title = regex.Match(html).Groups["title"].Value;
Console.Write(title);
}
Console.Read();
}
//读取某个网页的html信息
private string GetHtmlCode(string url)
{
string htmlCode;
HttpWebRequest webRequest = (System.Net.HttpWebRequest)System.Net.WebRequest.Create(url);
webRequest.Timeout = 30000;
webRequest.Method = "GET";
webRequest.UserAgent = "Mozilla/4.0";
webRequest.Headers.Add("Accept-Encoding", "gzip, deflate");
HttpWebResponse webResponse = (System.Net.HttpWebResponse)webRequest.GetResponse();
if (webResponse.ContentEncoding.ToLower() == "gzip")//如果使用了GZip则先解压
{
using (System.IO.Stream streamReceive = webResponse.GetResponseStream())
{
using (var zipStream =
new System.IO.Compression.GZipStream(streamReceive, System.IO.Compression.CompressionMode.Decompress))
{
using (StreamReader sr = new System.IO.StreamReader(zipStream, Encoding.Default))
{
htmlCode = sr.ReadToEnd();
}
}
}
}
else
{
using (System.IO.Stream streamReceive = webResponse.GetResponseStream())
{
using (System.IO.StreamReader sr = new System.IO.StreamReader(streamReceive, Encoding.Default))
{
htmlCode = sr.ReadToEnd();
}
}
}
return htmlCode;
}
//读取某个txt文本
var
file = File.Open(
"D:\\tx.txt"
, FileMode.Open);
List<
string
> txt =
new
List<
string
>();
using
(
var
stream =
new
StreamReader(file))
{
while
(!stream.EndOfStream)
{
txt.Add(stream.ReadLine());
}
}
var
array =
new
string
[10,10];
var
line = 0;
txt.ForEach(t => {
var
row = 0;
t.Split(
','
).ToList().ForEach(p => {
array.SetValue(p, line, row);
row++;
});
line++;
});
file.Close();