FireBug结合System.Net的数据抓取
以前一直用微软的Fiddler,现在受同事飞鸟(前端)的影响开始用FireFox的FireBug了,在此记录一下
打开FireFox,F12开启Firebug,在IP138的搜索页面搜一条合法数据
以前URL好像不显示参数的,不过也好,看起来直观-。-
1 ASCIIEncoding encoding = new ASCIIEncoding();
2 string postData = "ip=" + strId;
3 postData += ("&action=" + strPassword);
4
5 byte[] data = encoding.GetBytes(postData);
6
7 HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create("http://www.ip138.com/ips.asp");
8
9 myRequest.Method = "POST";
10 myRequest.ContentType = "application/x-www-form-urlencoded";
11 myRequest.ContentLength = data.Length;
12 Stream newStream = myRequest.GetRequestStream();
13
14 newStream.Write(data, 0, data.Length);
15 newStream.Close();
16
17 HttpWebResponse myResponse = (HttpWebResponse)myRequest.GetResponse();
18 StreamReader reader = new StreamReader(myResponse.GetResponseStream(), Encoding.Default);
19 string content = reader.ReadToEnd();
3 postData += ("&action=" + strPassword);
4
5 byte[] data = encoding.GetBytes(postData);
6
7 HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create("http://www.ip138.com/ips.asp");
8
9 myRequest.Method = "POST";
10 myRequest.ContentType = "application/x-www-form-urlencoded";
11 myRequest.ContentLength = data.Length;
12 Stream newStream = myRequest.GetRequestStream();
13
14 newStream.Write(data, 0, data.Length);
15 newStream.Close();
16
17 HttpWebResponse myResponse = (HttpWebResponse)myRequest.GetResponse();
18 StreamReader reader = new StreamReader(myResponse.GetResponseStream(), Encoding.Default);
19 string content = reader.ReadToEnd();
查询出来的结果
将结果保存到文本文档中
static void TestIP()
{
string filePath = @"d:\Ips.txt";
string strIp = string.Empty;
string strAd = string.Empty;
Random r = new Random();
TestClass tc = new TestClass();
FileInfo fi = new FileInfo(filePath);
System.Diagnostics.Stopwatch sw=new System.Diagnostics.Stopwatch();sw.Start();
for (int i = 1; i < 1001; i++)
{
int ip1 = r.Next(100, 255);
int ip2 = r.Next(0, 255);
int ip3 = r.Next(0, 255);
int ip4 = r.Next(0, 255);
strIp = ip1.ToString() + "." + ip2.ToString() + "." + ip3.ToString() + "." + ip4.ToString();
strAd = tc.GetAddressByIp(strIp);
using (FileStream fs = new FileStream(filePath, FileMode.Append))
using (TextWriter tw = new StreamWriter(fs))
{
tw.WriteLine("(" + i.ToString() + ") " + strIp + " " + strAd);
}
Console.WriteLine("第" + i.ToString() + "次抓取成功");
}
sw.Stop();
Console.WriteLine("总共抓取时间耗时:"+sw.ElapsedMilliseconds.ToString());
System.Diagnostics.Process.Start(filePath);
}
{
string filePath = @"d:\Ips.txt";
string strIp = string.Empty;
string strAd = string.Empty;
Random r = new Random();
TestClass tc = new TestClass();
FileInfo fi = new FileInfo(filePath);
System.Diagnostics.Stopwatch sw=new System.Diagnostics.Stopwatch();sw.Start();
for (int i = 1; i < 1001; i++)
{
int ip1 = r.Next(100, 255);
int ip2 = r.Next(0, 255);
int ip3 = r.Next(0, 255);
int ip4 = r.Next(0, 255);
strIp = ip1.ToString() + "." + ip2.ToString() + "." + ip3.ToString() + "." + ip4.ToString();
strAd = tc.GetAddressByIp(strIp);
using (FileStream fs = new FileStream(filePath, FileMode.Append))
using (TextWriter tw = new StreamWriter(fs))
{
tw.WriteLine("(" + i.ToString() + ") " + strIp + " " + strAd);
}
Console.WriteLine("第" + i.ToString() + "次抓取成功");
}
sw.Stop();
Console.WriteLine("总共抓取时间耗时:"+sw.ElapsedMilliseconds.ToString());
System.Diagnostics.Process.Start(filePath);
}