您当前的位置: 首页 >  c#

unity工具人

暂无认证

  • 3浏览

    0关注

    205博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

C#抓取网页HTML内容

unity工具人 发布时间:2021-07-08 23:31:13 ,浏览量:3

代码:

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
{
   ///   
   /// 公共方法类  
   ///   
   public class WebHandler
   {
      ///   
      /// 获取网页的HTML码  
      ///   
      /// 链接地址  
      /// 编码类型  
      ///   
      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(":", null);
        arrayList[1] = arrayList[1].Replace("", null);
        arrayList[1] = arrayList[1].Replace("", null);
        arrayList[1] = arrayList[1].Replace("", null);
        arrayList[1] = arrayList[1].Replace("", null);
        arrayList[1] = arrayList[1].Replace("", null);
        arrayList[1] = arrayList[1].Replace("", 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)

关注
打赏
1656671177
查看更多评论
立即登录/注册

微信扫码登录

0.0400s