C#抓取网页HTML内容

代码:

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;
    void Start() {
        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>", null);
        string result1 =Application.streamingAssetsPath+"/ 测试文件.txt";//结果保存到桌面
        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)

posted @ 2022-06-22 18:05  哒哒哒~~~  阅读(507)  评论(0编辑  收藏  举报