获取页面TDK信息

复制代码
  /// <summary>
    /// 获取页面html文本
    /// </summary>
    /// <param name="url"></param>
    /// <returns></returns>
    public string GetRequestHtml(string url)
    {
        string strResult = string.Empty;
        HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
        request.Method = "GET";
        request.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8";
        request.Headers.Add("Accept-Language", "zh-cn,zh;q=0.8,en-us;q=0.5,en;q=0.3");
        request.UserAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.131 Safari/537.36";
        HttpWebResponse response = (HttpWebResponse)request.GetResponse();
        Stream streamReceive = response.GetResponseStream();
        StreamReader streamReader = new StreamReader(streamReceive, Encoding.UTF8);
        strResult = streamReader.ReadToEnd();
        streamReader.Close();
        streamReader.Dispose();
        streamReader.Close();
        streamReader.Dispose();
        return strResult;
    }
复制代码
public void GetTDKInfo(HttpContext context)
    {
        string website = DataConvert.ToString(context.Request["website"]);
        string resHtml = GetRequestHtml(website);
        TDKInfo info = new TDKInfo();
        Regex reg = new Regex("title>(.+)<");
        //获取title
        Match match = reg.Match(resHtml);
        info.title = match.Groups[1].Value;
        Regex reg1 = new Regex("description(.+)content=\"(\\S+)\"");
        //获取description
        Match match1 = reg1.Match(resHtml);
        info.description = match1.Groups[2].Value;
        Regex reg2 = new Regex("keywords(.+)content=\"(\\S+)\"");
        //获取keyword
        Match match2 = reg2.Match(resHtml);
        info.keyword = match2.Groups[2].Value;
        Regex reg3 = new Regex("applicable-device(.+)content=\"(\\S+)\"");
        //获取meta application-device是否包含mobile,是否可以移动端自适应
        Match match3 = reg3.Match(resHtml);
        string meta = match3.Groups[2].Value;
        if (!string.IsNullOrEmpty(meta))
        {
            info.isAdaptive = match3.Groups[2].Value.Contains("mobile");
        }
        WriteString(context, true, "", info);
    }
复制代码

 


复制代码

 

posted @   极客船长  阅读(340)  评论(0编辑  收藏  举报
编辑推荐:
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
阅读排行:
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· 上周热点回顾(2.24-3.2)
点击右上角即可分享
微信分享提示