In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-30 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly introduces "how to use C#to crawl the content of the website page". In daily operation, I believe many people have doubts about how to use C#to crawl the content of the website page. Xiaobian consulted all kinds of materials and sorted out simple and easy to use operation methods. I hope to answer the doubts of "how to use C#to crawl the content of the website page"! Next, please follow the small series to learn together!
Grab the news column of Sina. com, as shown in the figure:
View source code using Google Chrome: Through analysis, we know that what we are looking for is between the following two tags:
The copy code is as follows:
Content...
As shown in the figure:
Content...
Use VS to create a website like the one shown:
We download network data mainly through the WebClient class to achieve.
Use the following source code to get our selection:
The copy code is as follows:
protected void Enter_Click(object sender, EventArgs e)
{
WebClient we = new WebClient(); //Mainly uses WebClient class
byte[] myDataBuffer;
myDataBuffer = we.DownloadData(txtURL.Text); //This method returns a byte array, so you need to define a byte[]
string download = Encoding.Default.GetString(myDataBuffer); //Encode the downloaded data
//Query the source code to get news content between two values
int startIndex = download.IndexOf("");
int endIndex = download.IndexOf("");
string temp = download.Substring(startIndex, endIndex - startIndex + 1); //intercept news content
lblMessage.Text = temp;//Show intercepted news content
}
The effect is as shown in the picture:
Finally: In addition to saving downloaded data as text, you can also save it as file type and stream type.
The copy code is as follows:
WebClient wc = new WebClient();
wc.DownloadFile(TextBox1.Text, @"F:\test.txt");
Label1.Text = "File download complete";
The copy code is as follows:
WebClient wc = new WebClient();
Stream s = wc.OpenRead(TextBox1.Text);
StreamReader sr = new StreamReader(s);
Label1.Text = sr.ReadToEnd();
At this point, on "how to use C#to achieve the content of the website page" learning is over, I hope to solve everyone's doubts. Theory and practice can better match to help you learn, go and try it! If you want to continue learning more relevant knowledge, please continue to pay attention to the website, Xiaobian will continue to strive to bring more practical articles for everyone!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.