用正则表达式,匹配网页元素

阅读更多
public static string GetHttp(string url)
        {
            HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create(url);

            httpWebRequest.ContentType = "application/json";
            httpWebRequest.Method = "GET";
            httpWebRequest.Timeout = 20000;
            HttpWebResponse httpWebResponse = (HttpWebResponse)httpWebRequest.GetResponse();
            StreamReader streamReader = new StreamReader(httpWebResponse.GetResponseStream());
            string responseContent = streamReader.ReadToEnd();

            httpWebResponse.Close();
            streamReader.Close();

            return responseContent;
        }

        static void Main(string[] args)
        {
            string url = "http://wap.10086.cn/czjf/result.jsp";
            string html = GetHttp(url);
            Regex r; // 声明一个 Regex类的变量
            r = new Regex("(.+?)

");   // 定义表达式
            Match m = r.Match(html); // 在字符串中匹配
            if (m.Success)
            {
                r = new Regex(">(.+?)<");  //找到后再定义表达式
                Match newString = r.Match(m.ToString());
                Console.Write(newString.ToString().Substring(1,10));
                Console.ReadKey();
            }
            else
            {
                Console.Write("未找到");
                Console.ReadKey();
            }
        }

通过匹配,找到自己想要的特定字符串

你可能感兴趣的:(用正则表达式,匹配网页元素)