PDA

View Full Version : giúp mình về project lọc nội dung web với



kervinngo
10-11-2009, 06:56
Hiện nay mình đang làm thực tập là viết báo điện tử tin tức tổng hợp bằng c#. việc viết trang web mình đã làm được rồi nhưng còn viết phầm mềm tự động lấy tin thì còn đang có nhiều khó khăn.

phần mềm của mình là lấy địa chỉ website từ csdl , rồi dựa trên những địa chỉ đó(địa chỉ này là RSS của các trang báo), tớ load trang đó về phân tích và đi vào từng item của trang rss (vì trang rss có nhiều item ,mổi item là `1 tin cụ thể ) để lấy cái link của item đó, rồi dựa trên link đó tớ tải trang chứa nội dung cụ thể của trang đó về. Việc này tớ làm được rồi .

sau đây là đoạn code tớ đã làm :

using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
usingusing System;
System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Web;
using System.Net;
using System.IO;
using System.Data.SqlClient;
using System.Xml;
namespace LAYTINTH
{
public partial class LayTinTH : Form
{
public LayTinTH()
{
InitializeComponent();
}

private string loadweb(string webfile)
{
WebResponse response = null;
StreamReader respStream = null;
try
{
WebRequest request = WebRequest.Create(webfile);
request.Timeout = 10000;
response = request.GetResponse();
respStream = new StreamReader(response.GetResponseStream());
return respStream.ReadToEnd();
}
catch (Exception ex)
{
return "";
response.Close();
respStream.Close();
}
}

private void ptxml()
{
string RSSFILE = lbpt.Text;
XmlDocument xml = new XmlDocument();
xml.LoadXml(lbpt.Text);
XmlNodeList n = xml.DocumentElement.ChildNodes;

foreach (XmlNode m in n)
{
if (m.Name == "channel")
{
foreach (XmlNode k in m.ChildNodes)
{
if (k.Name == "item")
{
foreach (XmlNode l in k.ChildNodes)
{
if (l.Name == "link")
{
lbxml.Text = l.InnerText;
taofilexml();
}
}
}

}
}

}
}

private void docfile()
{
SqlConnection con = new SqlConnection(@"Server=RIN;Integrated Security = True; Database = TTTH_Cong");
con.Open();
SqlCommand cmd = new SqlCommand("select CMBDT.RSSLINK from CMBDT where MACM = "+int.Parse(LBMACM.Text) +" and MABDT ="+int.Parse(LBMABDT.Text), con);
SqlDataReader dr = cmd.ExecuteReader();
while (dr.Read())
{
string RSSLINK = (string)dr["RSSLINK"];
if (RSSLINK == null)
{
break;
}
this.label3.Text = RSSLINK;
string RSSFILE = loadweb(RSSLINK);
this.lbpt.Text = RSSFILE;
if (RSSFILE == null)
{
MessageBox.Show(" không thể kết nối internet");
break;
}
ptxml();

}
dr.Close();
con.Close();

}

private void taofilerss()
{
string tem = loadweb(label3.Text);
FileStream fs = new FileStream("D:\\noidungrss.txt", FileMode.Append, FileAccess.Write, FileShare.Write);
fs.Close();
StreamWriter sw = new StreamWriter("D:\\noidungrss.txt", true, Encoding.UTF8);
sw.Write(tem);
sw.Close();
}

private void taofilexml()
{
string tem = loadweb(lbxml.Text);
lbxml2.Text = tem;
FileStream fs = new FileStream("D:\\noidungxml.txt", FileMode.Append, FileAccess.Write, FileShare.Write);
fs.Close();
StreamWriter sw = new StreamWriter("D:\\noidungxml.txt", true, Encoding.UTF8);
sw.Write(tem);
sw.Close();
}

private void button1_Click(object sender, EventArgs e)
{
if (cbdoisong.Checked == true)
{
string MACM = "7";
string MABDT = "1";
string MABDT1= "2";
LBMABDT.Text =MABDT;
LBMACM.Text = MACM;
docfile();
LBMABDT.Text = MABDT1; docfile();
//LBMABDT.Text = MABDT2; docfile();

}
if (cbthegioi.Checked == true)
{
string MACM = "2";
string MABDT = "1";
string MABDT1 = "2";
LBMABDT.Text = MABDT;
LBMACM.Text = MACM;
docfile();
LBMABDT.Text = MABDT1; docfile();
}
if(cbkhoahoc.Checked==true)
{
string MACM = "8";
string MABDT = "1";
LBMABDT.Text = MABDT;
LBMACM.Text = MACM;
docfile();

}
if (cbkinhdoanh.Checked == true)
{
string MACM = "3";
string MABDT = "1";
string MABDT1 = "2";
LBMABDT.Text = MABDT;
LBMACM.Text = MACM;
docfile();
LBMABDT.Text = MABDT1; docfile();
}
if (cbphapluat.Checked == true)
{
string MACM = "6";
string MABDT = "1";
LBMABDT.Text = MABDT;
LBMACM.Text = MACM;
docfile();

}
if (cbthethao.Checked == true)
{
string MACM = "5";
string MABDT = "1";
string MABDT1 = "2";
LBMABDT.Text = MABDT;
LBMACM.Text = MACM;
docfile();
LBMABDT.Text = MABDT1; docfile();
}
if (cbvanhoa.Checked == true)
{
string MACM = "4";
string MABDT = "1";
string MABDT1 = "2";
LBMABDT.Text = MABDT;
LBMACM.Text = MACM;
docfile();
LBMABDT.Text = MABDT1; docfile();
}
if (cbvitinh.Checked == true)
{
string MACM = "9";
string MABDT = "1";
LBMABDT.Text = MABDT;
LBMACM.Text = MACM;
docfile();

}
if (cbxahoi.Checked == true)
{
string MACM = "10";
string MABDT = "1";
string MABDT1 = "2";
LBMABDT.Text = MABDT;
LBMACM.Text = MACM;
docfile();
LBMABDT.Text = MABDT1; docfile();
}
if (cbchinhtri.Checked == true)
{
string MACM = "1";
string MABDT = "2";
LBMABDT.Text = MABDT;
LBMACM.Text = MACM;
docfile();

}
MessageBox.Show("hoàn thành tải tin về, vui lòng test và xử lý dữ liệu !");
}

}
}


Vấn đề của tớ bây giờ là làm sao phân tích html của các tin vừa lấy về,lọc lấy nội dung của cac tin đó save vào cơ sỡ dữ liệu . Tớ chưa biết phải làm thế nào , tớ nghĩ đã có hàm loadweb, giờ chỉ cần hàm locnoidung và kiểm tra xem noi dung đó đã có trong csdl hay chưa ? nếu có thì ko save nữa , và căn cứ được xem là mốc thời gian của các tin ... bạn nào rành về cái này làm ơn chỉ tớ với , tớ cần gấp để hoàn thành phần winform này. thanks những ai đã đọc bài.