Asp.net 静态页面生成(3)----动态页生成之 生成爬虫

本文介绍了一种通过爬虫技术批量生成动态网页静态副本的方法。该方法利用C#编程实现了一个名为Creater的类,能够从数据库中获取文章ID,并为每篇文章生成带有Create=true参数的URL。使用System.Net.WebClient组件发起HTTP请求,从而触发动态页面的静态化过程。

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

http://www.cnblogs.com/michael555cdj/archive/2008/03/23/1118819.html

根据前面讲解的生成方法,需要访问动态页面戴上 Create=true 参数就可以生成了。但是一个一个页面的访问是不是有点累啊
那我们就可以在生成管理器里面写个爬虫去爬遍每一个页面就可以达到生成的目的了。
由于采用http访问生成,爬虫可以在部署在任何位置,实例代码如下

public class Creater
{
    
public Creater(string serverName)
    
{
        
this._ServerName = serverName;
    }

    
private string _ServerName;
    
public string ServerName
    
{
        
get
        
{
            
return _ServerName;
        }

    }

    
public int CreateArticlePage()
    
{
        
string sqlStr = "SELECT ArticelId FROM Article";
        DataSet ds 
= DataBase.ExecuteDataset(sqlStr);
        System.Collections.Generic.List
<string> urls = new System.Collections.Generic.List<string>();
        
if (ds != null && ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
        
{
            
for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
            
{
                urls.Add(
"http://" + ServerName + "/Article/Show.aspx?ArticleId=" + ds.Tables[0].Rows[i].ToString());
            }

        }

        
return CreateHtml(urls);
    }

    
private int CreateHtml(System.Collections.Generic.List<string> urls)
    
{
        
int result = 0;
        System.Net.WebClient client 
= new System.Net.WebClient();
        
for (int i = 0; i < urls.Count; i++)
        
{
            System.IO.Stream stream 
= client.OpenRead(urls[i]);
            System.IO.StreamReader sr 
= new System.IO.StreamReader(stream);
            result 
+= int.Parse(sr.ReadToEnd());
        }

        
return result;
    }

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值