62,271
社区成员
发帖
与我相关
我的任务
分享
string url = "http://www.qq.com/";
string path = Server.MapPath("index.html");
StringBuilder sb = new StringBuilder();
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
WebResponse rep = req.GetResponse();
using (StreamReader sr = new StreamReader(rep.GetResponseStream())
{
sb.Append(sr.ReadToEnd());
}
using (StreamWriter sw = new StreamWriter(path,false))
{
sw.WriteLine(sb.ToString());
}
}


class Program
{
static void Main(string[] args)
{
List<Action> actions = new List<Action>();
actions.Add(() => Console.WriteLine("A1"));
actions.Add(() => Console.WriteLine("A2"));
actions.Add(() => Console.WriteLine("A3"));
actions.Add(() => Console.WriteLine("A4"));
foreach (var action in actions)
{
ThreadPool.QueueUserWorkItem(state => action(), null); }
Console.Read();
}
}
DateTime star = DateTime.Now;
StringBuilder sb = new StringBuilder();
Thread td = null;
foreach (string result in ilist)
{
td = new Thread(new ParameterizedThreadStart(Save2));
/*
为什么这里用多线程来采集的话总是没有采集完,只有7,8个
如果改用Save(result, Server.MapPath(@"collection/" + datetime + ".html"));这样直接保存的话是有全部采集到,总共241个,这是为什么呢?
*/
td.Start(result);
}
DateTime end = DateTime.Now;
Response.Write((end - star).TotalMilliseconds);
ThreadPool.QueueUserWorkItem(new Action(()=>{
//todo: 这里写你要执行的方法的代码
}));
一页一页的抓取,主要是变化url里面的参数。
比如:
http://www.baidu.com/s?wd=asd&pn=0&usm=2 第1页
http://www.baidu.com/s?wd=asd&pn=10&usm=2 第2页
生成对应的url即可,然后扔给采集代码里面的url
线程,声明下,然后调用对应方法就可以
Thread thread = new Thread(new ThreadStart(方法));
thread.Start();
多线程多声明几次就可以。