Thứ Năm, 9 tháng 8, 2012

Lấy tin tự động - Đọc nội dung chi tiết

Cách làm của mình là đọc nội dung bài viết đó dưới dạng html, sau đó phân tích và cắt chuỗi đế lấy nội dung. Cách làm này chưa thực sự tốt vì với mỗi website khác nhau lại có 1 cầu trúc khác nhau, và nếu trang web mình lấy thông tin thay đổi cấu trúc thì mình sẽ lại phải viết lại code. Bài này mình sẽ lấy nội dung của trang vnexpress.net.
1. Hàm đọc nội dung của web

public string GetWebContent(string Address)

    {

        string txOutput = "";

        try

        {

            if (Address != "")

            {

                System.Net.WebClient MyClient = new System.Net.WebClient();

                System.IO.Stream MyStream = MyClient.OpenRead(Address);

                System.IO.StreamReader MyStreamReader = new System.IO.StreamReader(MyStream);

                string NewLine;

                while ((NewLine = MyStreamReader.ReadLine()) != null)

                {

                    txOutput += NewLine + "\r\n";

                }

            }

        }

        catch { }

        return txOutput;

    }

 2. Hàm đếm số kỹ tự của chuỗi

static int CountChars(string value)

    {

        int result = 0;

        bool lastWasSpace = false;

 

        foreach (char c in value)

        {

            if (char.IsWhiteSpace(c))

            {

                if (lastWasSpace == false)

                {

                    result++;

                }

                lastWasSpace = true;

            }

            else

            {

                result++;

                lastWasSpace = false;

            }

        }

        return result;

    }

 3. Hàm tìm và thay đường dẫn tương đối thành tuyệt đối của anh và liên kết

private string GetPicture(string Content)

    {

        string result = "";

        string img = "";

       

        if (Content.IndexOf("<IMG") > -1)

        {

            if (Content.IndexOf("src=") > -1)

            {

                Content = Content.Replace("src=\"", "class=\"Anh\" src=\"http://vnexpress.net/");

            }

        }

        if (Content.IndexOf("<A") > -1)

        {

            if (Content.IndexOf("href=") > -1)

            {

                Content = Content.Replace("href=\"", " href=\"http://vnexpress.net/");

            }

        }

        result = Content;

        return result;

    }

 4. Hàm cắt chuỗi để lầy nội dung bài viết

private string ReasNews(string urlID)

    {

        try

        {

            string strConTent = GetWebContent(urlID) + "";

            string phandau = strConTent.Substring(0, strConTent.IndexOf("class=\"content\"") + CountChars("class=\"content\"") + 1);

            strConTent = strConTent.Replace(phandau, "");

            string phanduoi = strConTent.Substring(strConTent.IndexOf("othernews-header") - 13, strConTent.Length - strConTent.IndexOf("othernews-header"));

            strConTent = strConTent.Replace(phanduoi, "");

            strConTent = GetPicture(strConTent);

            ltlConTent.Text = strConTent;

        }

        catch { }

    }

 Ví dụ bạn dùng ReasNews("http://vnexpress.net/GL/Vi-tinh/2010/03/3BA1944F/") sẽ trả về nội dung chi tiết của bài viết "7 công cụ mà điện thoại di động sẽ thay thế"

Chú ý  để đọc nội dung trang web bạn  cần thêm những thư viện sau

using System.Net;
using System.IO;
using System.Xml;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;

Cách làm này chưa tối ưu nhưng ở thời điểm hiện tại thì chạy tốt


#End

Không có nhận xét nào:

Liên kết quảng cáo :