---
title: "从网页抓取数据的一般方法"
author: "csstome"
date: "2007-12-28T01:08:00Z"
tags: []
slug: "untitled"
draft: false
lastmod: "2007-12-28T01:08:00Z"
id: 1033160
url: "https://csstome.iteye.com/blog/1033160"
---

首先要了解对方网页的运行机制，这可以用httpwacth或者httplook来看一下http发送和接收的数据。这两个工具应该说是比较简单易懂的。这里就不再介绍了。主要关注的内容是header和post的内容。一般会包括cookie，Referer页面和其他一些乱其八糟可能看不懂的变量，还有就是正常交互的参数，比如需要post或者get的querystring所包含的东西。

httplook和httpwacth 网上有很多下载的，这里推荐使用httpwach，因为可以直接嵌入到ie中，个人觉得这个比较好用。这两个工具可以到我上传在csdn的资源中下载，地址为

[http://download.csdn.net/user/jinjazz](http://download.csdn.net/user/jinjazz)

这里简单给出一段可以抓取数据的c#代码，比如登录某个网站，获取登录成功后的html代码，供数据分析使用。

![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)private void login()  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif) ... {  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif) System.Net.WebClientwb = new System.Net.WebClient();  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif) System.Collections.Specialized.NameValueCollectionheader = new System.Collections.Specialized.NameValueCollection();  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif) header.Add( " Cookie " , " czJ\_cookietime=2592000;czJ\_onlineusernum=1651;czJ\_sid=w4bGJd " );  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif) header.Add( " Referer " , @" http://www.test\_by\_jinjazz.com.cn/bbs/login.php " );  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif) wb.Headers.Add(header);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif) System.Collections.Specialized.NameValueCollectiondata = new System.Collections.Specialized.NameValueCollection();  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif) data.Add( " formhash " , " ebd2faac " );  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif) data.Add( " referer " , " http://www.test\_by\_jinjazz.com.cn/bbs/search.php " );  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif) data.Add( " loginfield " , " username " );  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif) data.Add( " username " , " jinjazz " );  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif) data.Add( " password " , " 999 " );  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif) data.Add( " questionid " , " 0 " );  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif) data.Add( " answer " , "" );  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif) data.Add( " cookietime " , " 2592000 " );  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif) data.Add( " loginmode " , "" );  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif) data.Add( " styleid " , "" );  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif) data.Add( " loginsubmit " , " 提交 " );  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif) byte \[]b = wb.UploadValues( " http://www.test\_by\_jinjazz.com.cn/bbs/login.php " , " Post " ,data);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif) string strData = System.Text.Encoding.Default.GetString(b);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif) Console.WriteLine(strData);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif) }  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)

以上代码除了三个url之外其他数据都是真实的，其中header和data中的参数和值都是用httpwatch来监测后得到。