C# 数据采集进展(实验储备)

本文分享了一个使用C#编写的控制台应用案例,该应用能够从搜狐新闻网站上抓取评论数据,并将其存储到数据库中。通过正则表达式匹配获取所需信息,实现了自动化批量抓取。

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

 写了一个控制台应用程序, 用了2个小时时间一共从sohu网站上抓取30000多条记录.截图如下.

数据库截图

离十几万的距离又近了一步.

 

using System;
using System.Data;
using System.IO;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using System.Text;
using System.Text.RegularExpressions;
using System.Net;
using System.Collections.Generic;

/// <summary>
/// test 的摘要说明
/// </summary>
/// 


    
public class test
    
{
        
        
//private string requestUrl;
        
//private int resultCount;
        private static string information;
        
private static HttpWebRequest httpwRequest;
        
private static HttpWebResponse httpwResponse;
        
//private string pageText = "";
        
//private string[] regexs;
        private static int count;
        
private static  SqlConnection connection;
        
//public MatchCollection[] matchs;


        
public static void Main()
        
{
            
string headerRegex = "(?<=ti8 fc_3333'>)[^<]{10,}";
            
string textRegex = "(?<=528 class='break'>)[^/]*</td></tr>";
            MatchCollection mcHeader;
            MatchCollection mcText;
            connection 
= new SqlConnection(@"Data Source=BAKERS;Initial Catalog=idea;User ID=sa;Password=123456");
            
           
// string header = "";

            
for (int j = 1; j < 1599; j++)
            
{
                
try
                
{
                    count 
= 0;
                    
string page;
                    page 
= j.ToString();
                    
string requestUrl = "http://comment2.news.sohu.com/viewcomments.action?id=242136287&pageNumber=" + page;
                    
//string requestUrl = "http://comment2.news.sohu.com/viewcomment.action?id=12146627";
                    httpwRequest = (HttpWebRequest)WebRequest.Create(requestUrl);
                    httpwResponse 
= (HttpWebResponse)httpwRequest.GetResponse();
                    information 
= TextContent(httpwResponse);
                    mcHeader 
= Regex.Matches(information, headerRegex);
                    mcText 
= Regex.Matches(information, textRegex);
                   
// Response.Write(mcHeader.Count.ToString() + "  " + mcText.Count.ToString());
                    for (int i = 0; (i < mcText.Count) && (i < mcHeader.Count); i++)
                    
{
                        count
++;
                        Console.WriteLine(
"page:" + j.ToString() + "  count:" + count.ToString() + " :" + Regex.Replace(mcHeader[i].Value, "(<[^>]*>)|'"""));
                        Console.WriteLine(Regex.Replace(mcText[i].Value, 
"(<[^>]*>)|'"""));
                        ResultInsert(Regex.Replace(mcHeader[i].Value, 
"(<[^>]*>)|'"""), Regex.Replace(mcText[i].Value, "(<[^>]*>)|'"""));
                    }

                    
// Label1.Text = header;
                    
//}
                    
//  Label1.Text = count.ToString() + "条记录已经被抓取!";

                }

                
catch (Exception ee)
                
{
                    Console.WriteLine(ee.Message);
                }

            }

           
        }

        
public static void ResultInsert(string header, string context)
        
{
            
string sql = "insert into sohu(header,context,other)values('" + header + "','" + context + "',2)";
            SqlCommand command 
= new SqlCommand(sql, connection);
            
if (connection.State == ConnectionState.Closed)
            
{
                command.Connection.Open();
            }

            command.ExecuteNonQuery();
          
        }

       
        
public static Encoding GetEncoding(HttpWebResponse response)
        
{
            
string name = response.ContentEncoding;
            Encoding code 
= Encoding.Default;
            
if (name == "")
            
{
                
string contentType = response.ContentType;
                
if (contentType.ToLower().IndexOf("charset"!= -1)
                
{
                    name 
= contentType.Substring(contentType.ToLower().IndexOf("charset="+ "charset=".Length);
                }

            }


            
if (name != "")
            
{
                
try
                
{
                    code 
= Encoding.GetEncoding(name);
                }

                
catch { }
            }

            
return code;
        }



        
public static string TextContent(HttpWebResponse response)
        
{
            
string buffer = "", line;

            Stream stream 
= response.GetResponseStream();
            StreamReader reader 
= new StreamReader(stream, GetEncoding(response));


            
while ((line = reader.ReadLine()) != null)
            
{

                buffer 
+= line + " ";
            }

            stream.Close();
            response.Close();
            
return buffer;
        }



    }




Soukey采摘网站数据采集软件是一款基于.Net平台的开源软件,也是网站数据采集软件类型中唯一一款开源软件。尽管Soukey采摘开源,但并不会影响软件功能的提供,甚至要比一些商用软件的功能还要丰富。Soukey采摘当前提供的主要功能如下: 1. 多任务多线程数据采集,支持POST方式; 2. 可采集Ajax页面; 3. 支持Cookie,支持手工登录采集数据; 4. 支持采集事务; 5. 支持数据自动及手工导出,导出格式为:文本、Excel、Access、MSSql、Mysql等; 6. 支持在线发布数据; 7. 支持导航网址的采集,导航深度不限; 8. 支持自动翻页; 9. 支持文件下载,可以采集图片、Flash及其他文件; 10. 支持采集结果数据的加工,包括替换、附前缀后缀、截取等操作,支持正则; 11. 采集网址定义不仅支持基本参数定义,也可外接字典数据作为网址参数,进行数据采集; 12. 支持一个任务多实例运行; 13. 提供计划任务,计划任务支持Soukey采集任务、外部可执行文件任务、数据库存储过程任务; 14. 计划任务执行周期支持每天、每周及自定义运行间隔;最小单位为:半小时; 15. 支持任务触发器,即可在采集任务完成后,自动触发执行其他任务(包括可执行文件或存储过程)。 16. 完善的日志功能:系统日志、任务执行日志、出错日志等等; 17. 系统提供MINI浏览器可用于捕获Cookie或POST数据; Soukey采摘并不限制您是否商用此软件,源码完全开放,
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值