C#抓取网页HTML内容

56 篇文章 7 订阅

代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Net;
using System.Text;
using System.IO;
using System.Text.RegularExpressions;

namespace Web
{
   /// <summary>  
   /// 公共方法类  
   /// </summary>  
   public class WebHandler
   {
      /// <summary>  
      /// 获取网页的HTML码  
      /// </summary>  
      /// <param name="url">链接地址</param>  
      /// <param name="encoding">编码类型</param>  
      /// <returns></returns>  
      public static string GetHtmlStr(string url, string encoding)
      {
         string htmlStr = "";
         try
         {
            if (!String.IsNullOrEmpty(url))
            {
               WebRequest request = WebRequest.Create(url);            //实例化WebRequest对象  
               WebResponse response = request.GetResponse();           //创建WebResponse对象  
               Stream datastream = response.GetResponseStream();       //创建流对象  
               Encoding ec = Encoding.Default;
               if (encoding == "UTF8")
               {
                  ec = Encoding.UTF8;
               }
               else if (encoding == "Default")
               {
                  ec = Encoding.Default;
               }
               StreamReader reader = new StreamReader(datastream, ec);
               htmlStr = reader.ReadToEnd();                  //读取网页内容  
               reader.Close();
               datastream.Close();
               response.Close();
            }
         }
         catch { }
         return htmlStr;
      }
   }  
   
}

调用方法后可以看到,成功获取到了网址中的Html内容
在这里插入图片描述
好玩儿!

我们可以以此来提取某网页中的一些数值
测试案例

using System.Collections;
using System.Collections.Generic;
using System.IO;
using UnityEngine;
using UnityEngine.UI;
using Web;

public class WebTest : MonoBehaviour
{
    public Text text;
    private string result1;
     private void Awake()
    {
        InitPath();
    }

    private void InitPath()
    {
        var path = Application.streamingAssetsPath;
        var filename = "测试文件.txt";
        result1 = path + "/ " + filename;//结果保存到桌面

        if (!Directory.Exists(path))
        {
            try
            {
                Directory.CreateDirectory(path);
            }
            catch (Exception e)
            {

            }
        }

        //判断文件夹是否存在
        if (!File.Exists(result1))
        {
            //创建文件
            try
            {
                File.Create(result1);
            }
            catch (Exception e)
            {
            }
        }
    }
    
    void Start() {
       GetText();
    }
public void GetText()
    {
        text.text = WebHandler.GetHtmlStr("https://blog.csdn.net/weixin_45023328/article/details/118584968", "UTF8");
        string[] arrayList = text.text.Split('≡');
        arrayList[1] = arrayList[1].Replace("&#xff1a", null);
        arrayList[1] = arrayList[1].Replace("</ span >", null);
        arrayList[1] = arrayList[1].Replace("<span class=\"token number\">", null);
        arrayList[1] = arrayList[1].Replace("</span><span class=\"token operator\">", null);
        arrayList[1] = arrayList[1].Replace("<span class=\"token operator\">", null);
        arrayList[1] = arrayList[1].Replace("</span>", null);
        arrayList[1] = arrayList[1].Replace("<span class=\"token punctuation\">", null);

        string[] arrayList2 = arrayList[1].Split('|');


        foreach (var item in arrayList2)
        {
            Debug.Log(item);
        }
       

        FileStream fs = new FileStream(result1, FileMode.Append);
        StreamWriter wr = null;
        wr = new StreamWriter(fs);
        wr.WriteLine(arrayList[1]);
        wr.Close();
    }
}

在这里插入图片描述

原文地址:[https://www.cnblogs.com/yunfeifei/p/3842761.html](https://www.cnblogs.com/yunfeifei/p/3842761.html)

  • 2
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值