Tôi đã tạo một trình thu thập dữ liệu web đơn giản nhưng tôi muốn thêm chức năng đệ quy để mọi trang được mở tôi có thể nhận được các url trong trang này, nhưng tôi không biết làm cách nào tôi có thể làm điều đó và tôi cũng muốn bao gồm chủ đề để làm cho nó nhanh hơn ở đây nó là mã của tôiTrình thu thập dữ liệu web đơn giản trong C#
namespace Crawler
{
public partial class Form1 : Form
{
String Rstring;
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
WebRequest myWebRequest;
WebResponse myWebResponse;
String URL = textBox1.Text;
myWebRequest = WebRequest.Create(URL);
myWebResponse = myWebRequest.GetResponse();//Returns a response from an Internet resource
Stream streamResponse = myWebResponse.GetResponseStream();//return the data stream from the internet
//and save it in the stream
StreamReader sreader = new StreamReader(streamResponse);//reads the data stream
Rstring = sreader.ReadToEnd();//reads it to the end
String Links = GetContent(Rstring);//gets the links only
textBox2.Text = Rstring;
textBox3.Text = Links;
streamResponse.Close();
sreader.Close();
myWebResponse.Close();
}
private String GetContent(String Rstring)
{
String sString="";
HTMLDocument d = new HTMLDocument();
IHTMLDocument2 doc = (IHTMLDocument2)d;
doc.write(Rstring);
IHTMLElementCollection L = doc.links;
foreach (IHTMLElement links in L)
{
sString += links.getAttribute("href", 0);
sString += "/n";
}
return sString;
}
Bỏ phiếu để đóng câu hỏi không có phạm vi. Vui lòng xem xét tìm kiếm "trình thu thập dữ liệu web trong C#" và tinh chỉnh câu hỏi của bạn để được nhắm mục tiêu nhiều hơn. –
Bạn chắc chắn không muốn sử dụng đệ quy vì bạn sẽ không thể giữ trạng thái của Internet trên ngăn xếp cục bộ. Bạn có thể sử dụng Ngăn xếp như Tom được đề xuất, nhưng bạn nên đảo ngược thứ tự mà bạn thêm AbsoluteUris vào được thu thập dữ liệu, nếu không thì bản chất của ngăn xếp sẽ khiến bạn thu thập dữ liệu từ cuối trang và nếu bạn định viết trình thu thập thông tin, bạn cần có hành vi truy xuất phù hợp với trình duyệt càng chặt chẽ càng tốt. Hàng đợi là một lựa chọn tốt hơn. Một PriorityQueue thậm chí còn tốt hơn. –
PriorityQueue https://svn.arachnode.net/svn/arachnodenet/trunk/Structures/PriorityQueue.cs username/password: công cộng/Public –