zoukankan      html  css  js  c++  java
  • 使用C#正则表达式获取必应每日图片地址

        微软的Bing搜索引擎首页每天都会提供了一些有趣的图片,下面使用正则表达式获取图片的地址,不管是在手机app还是在网站上都是很好的图片素材,而且每天更新,非常不错。

        首先访问微软的API,该地址返回的是xml文本,获取xml文本后使用正则表达式匹配url节点中的内容,加上必应主页链接即可获得图片的真实网址。下面是获取网址的全部代码。

        

    string InfoUrl = "http://cn.bing.com/HPImageArchive.aspx?idx=0&n=1";
    HttpWebRequest request = (HttpWebRequest)WebRequest.Create(InfoUrl);
    request.Method = "GET"; request.ContentType = "text/html;charset=UTF-8";
    string XmlString;
    using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
    {
       Stream myResponseStream = response.GetResponseStream();
       using (StreamReader myStreamReader = new StreamReader(myResponseStream, Encoding.UTF8))
       {
           XmlString = myStreamReader.ReadToEnd();
       }
    }
    // 定义正则表达式用来匹配标签
    Regex regImg = new Regex("<Url>(?<imgUrl>.*?)</Url>", RegexOptions.IgnoreCase);
    // 搜索匹配的字符串
    MatchCollection matches = regImg.Matches(XmlString);
    // 取得匹配项列表
    string ImageUrl = "http://www.bing.com" + matches[0].Groups["imgUrl"].Value;
    background_image.Src = ImageUrl;

    同步发表于我的博客,使用C#正则表达式获取必应每日图片地址

  • 相关阅读:
    AJAX注册
    文件上传加水印
    邮箱
    AJAX完整操作
    跨窗体操作
    容器布局
    EF异常类
    SQL查出字段横向拼接,如:1,2,3,4
    asp.net三层结构中,SQL助手类DbHelperSQL
    正则抓取页面信息
  • 原文地址:https://www.cnblogs.com/NuclearBoy/p/6748049.html
Copyright © 2011-2022 走看看