none
HttpwebReponse取部分内容问题 RRS feed

  • 问题

  • 用HttpwebReponse取一个网页的部分内容

    如 :这个网页地址 http://seo.chinaz.com/?host=www.fjkaineng.com&c=1&m=1

    要这个网页的红色框里的内容,如下图:

    


    要怎么做才可以获得红色框里的内容?

    我只懂得取得整个网页的内容,懂不得要怎么处理文件流获得部分内容?能给个列子吗(结合JS或Jq等都可以的)?

     protected string httpRequsetToString()

            {
                string str = null;
                try
                {
                    HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create("http://seo.chinaz.com/?host=www.fjkaineng.com&c=1&m=1");
                    req.Method = "GET";

                    //获取本次的回馈,先创建接收回馈的类HTTPWEBRESPONSE
                    HttpWebResponse myHttpWebResponse = (HttpWebResponse)req.GetResponse();
                    //创建接收回馈的字节流类
                    Stream receiveStream = myHttpWebResponse.GetResponseStream();//得到回写的字节流 
                    Encoding encode = System.Text.Encoding.UTF8;
                    StreamReader readStream = new StreamReader(receiveStream, encode);

                    str = readStream.ReadToEnd();
                    readStream.Close();
                }
                catch (Exception Ex)
                {
                    throw new Exception(Ex.Message);
                }

                return str;
            }

    2013年9月15日 3:25

全部回复

  • 你好,

        我建议你去尝试HTML Agility Pack.

    HTML Agility Pack:簡單好用的快速 HTML Parser

    http://msdn.microsoft.com/zh-tw/ee787055.aspx

    例子:

                //HtmlDocument htmlDocument = new HtmlDocument();

                //htmlDocument.LoadHtml(str);

                HtmlWeb webClient = new HtmlWeb();

         HtmlDocument doc = webClient.Load("http://www.w3.org/");

     

        HtmlNodeCollection nodes = doc.DocumentNode.SelectNodes("/html[1]/body[1]/div[1]/div[2]/div[3]/div[2]/div[1]/div[1]/div[1]/div");

        foreach (HtmlNode node in nodes)

        {

            Console.WriteLine(node.InnerText.Trim());

    }


    <THE CONTENT IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, WHETHER EXPRESS OR IMPLIED>
    Thanks
    MSDN Community Support

    Please remember to "Mark as Answer" the responses that resolved your issue. It is a common way to recognize those who have helped you, and makes it easier for other visitors to find the resolution later.

    2013年9月16日 7:40
    版主