C# 网络爬虫

本博客介绍了一个使用C#进行数据采集的项目,该项目从51job网站上抓取工作列表信息,并将数据存储到SQL Server数据库中。采集过程采用多线程并利用HtmlAgilityPack和Dapper插件解析网页,展示了如何处理线程间通信、数据持久化及UI更新。
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
using DataCollect.Model;
using HtmlAgilityPack;//引入插件
using Dapper;//引入插件

namespace DataCollect.UI
{
    public partial class MainForm1 : DevExpress.XtraEditors.XtraForm
    {
        CancellationTokenSource cts = new CancellationTokenSource();
        List<Job> list = new List<Job>();
        public MainForm1()
        {
            InitializeComponent();
        }

        private void MainForm1_Load(object sender, EventArgs e)
        {

        }

        private void simpleButton1_Click(object sender, EventArgs e)
        {
            //线程与线程之前不能直接访问(线程是独立的任务片段)
            Task task1 = new Task(Collect);//创建一个线程任务
            task1.Start();
        }

        /// <summary>
        /// 数据采集
        /// </summary>
        private void Collect()
        {
            int index = 1;
            int pageCount=0;
            do
            {
                //HtmlAgilityPack.HtmlDocument htmlDocument=DataLoad(index);
                var htmlDocument = DataLoad(index);
                if (index == 1)
                {
                    //根据查询节点信息,获取某个节点
                    HtmlNode htmlNode = htmlDocument.DocumentNode.SelectSingleNode("//input[@id='hidTotalPage']");//标答节点//根节点
                    pageCount = int.Parse(htmlNode.Attributes["value"].Valu
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值