c#抓取浏览器网页代码数据(winform)

为应对月底游戏新补丁发布,通过编写WinForm应用实现自动从特定网站抓取并解析网页数据,最终将有效信息存储至数据库。解决了3万多条记录的人工处理难题,仅用11小时即完成全部数据的抓取。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

昨天主管突然找我,说月底有个游戏要发新补丁,但是没有新增部分的资料,找到一个网站却不知道怎么下载到我们的数据库中.
我看了一下,网页为了防抓取,都是用js来生成内容,不过还是让我找到具体位置,所有物品有3万多,需要的还要过滤,有用的只有3千多,人工来做几乎不太现实的,于是写了一个winform来抓它的信息来写到数据库中
动作在DocumentCompleted事件中完成.



        private const string SQL_DATA = "select * from Table1";
        private const string SQL_INSERT_1 = "insert into Table1 (WebID,Name,NeedLevel,Content) values (";
        private const string Sql_INSERT_2 = ")"; None.gif        
private   void  webBrowser1_DocumentCompleted( object  sender, WebBrowserDocumentCompletedEventArgs e)
ExpandedBlockStart.gifContractedBlock.gif        
dot.gif {
InBlock.gif            
string mydocument = webBrowser1.DocumentText;
InBlock.gif
InBlock.gif            
//listBox1.Items.Add(mydocument);
InBlock.gif
            string SQL_INSERT = string.Empty;
//取得页面中的物品等级
InBlock.gif            
int mylevel = GetLevel(mydocument);
//取得页面中物品名称
InBlock.gif            
string myname = GetName(mydocument);
InBlock.gif            
if (!string.IsNullOrEmpty(myname))
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                SQL_INSERT 
= SQL_INSERT_1 + ID.ToString() + ",'" + myname + "'," + mylevel + ",'" + mydocument + "'" + Sql_INSERT_2;
InBlock.gif
InBlock.gif                SqlConnection cn 
= new SqlConnection();
InBlock.gif                cn.ConnectionString 
= SQL_CONNECTION;
InBlock.gif                SqlCommand sqlcmd 
= new SqlCommand(SQL_INSERT, cn);
InBlock.gif                cn.Open();
InBlock.gif                
try
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    
int tmp = sqlcmd.ExecuteNonQuery();
ExpandedSubBlockEnd.gif                }

InBlock.gif                
catch
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    
throw new Exception("no good");
ExpandedSubBlockEnd.gif                }

InBlock.gif                sqlcmd.Dispose();
InBlock.gif                cn.Close();
ExpandedSubBlockEnd.gif            }

ExpandedBlockEnd.gif        }

 取得物品名称和等级:

None.gif          private   string  GetName( string  mydocument)
ExpandedBlockStart.gifContractedBlock.gif        
dot.gif {
InBlock.gif            
if (string.IsNullOrEmpty(mydocument)) return string.Empty;
InBlock.gif            
int pos_start = mydocument.IndexOf(".html\">", 0);
InBlock.gif
            if (pos_start == -1return string.Empty;
InBlock.gif            
int pos_end = mydocument.IndexOf("</a></div>"0);
InBlock.gif            
if (pos_end == -1return string.Empty;
InBlock.gif            
if (pos_start >= pos_end) return string.Empty; 
InBlock.gif            
string name = string.Empty;
InBlock.gif            name 
= mydocument.Substring(pos_start + 7, pos_end - pos_start - 7);
InBlock.gif            
return name;
ExpandedBlockEnd.gif        }

None.gif
None.gif        
private   int  GetLevel( string  mydocument)
ExpandedBlockStart.gifContractedBlock.gif        
dot.gif {
InBlock.gif            
if (string.IsNullOrEmpty(mydocument))return 0;
InBlock.gif            
int pos_start = mydocument.IndexOf("需要等级"0);
InBlock.gif            
if (pos_start == -1return 0;
InBlock.gif            
int pos_end = mydocument.IndexOf("</div>", pos_start);
InBlock.gif            
if (pos_end == -1return 0;
InBlock.gif            
if (pos_start >= pos_end) return  0;
InBlock.gif            
string Level = "0";
InBlock.gif            Level 
= mydocument.Substring(pos_start + 5, pos_end - pos_start - 5);
InBlock.gif            
int intleve = 0;
InBlock.gif            
try
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                intleve 
= Convert.ToInt32(Level);
ExpandedSubBlockEnd.gif            }

InBlock.gif            
catch
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
ExpandedSubBlockEnd.gif            }

InBlock.gif            
finally
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
ExpandedSubBlockEnd.gif            }

InBlock.gif                
return intleve;
ExpandedBlockEnd.gif        }


无法判断浏览器是否完成加载,所以用timer控件来完成:

None.gif          private   const   int  START_ID  =  1 ;
None.gif        
private   const   int  END_ID  =  4 0000 ; // 32999;
None.gif
         private   static   int  ID  =  1 ;
None.gif
None.gif        
private   void  timer1_Tick( object  sender, EventArgs e)
ExpandedBlockStart.gifContractedBlock.gif        
dot.gif {
InBlock.gif            
if (ID < END_ID && !webBrowser1.IsBusy)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                ID 
= ID + 1;
InBlock.gif                webBrowser1.Navigate(
"http://xxxx.com/xx.php?id=" + ID.ToString());
InBlock.gif
InBlock.gif                
//listBox1.Items.Add(ID.ToString() + webBrowser1.DocumentText);
InBlock.gif
InBlock.gif                
//InsertIntoDB(webBrowser1.DocumentText, ID);
InBlock.gif
                textBox4.Text = ID.ToString();
ExpandedSubBlockEnd.gif            }

ExpandedBlockEnd.gif        }

timer的interval控制在1000ms,40000条数据用了11个多小时,从昨天晚上10点到今天早上,刚来的时候看到数据全都乖乖的在数据库呆着了.呵呵,搞定,交差...
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值