C#自动登录、浏览网页,轻松抓取数据!

优采云 发布时间: 2023-06-20 00:58

  2023年的今天,随着互联网的快速发展,网络爬虫已经成为了一种非常流行的技术。而对于大多数开发者来说,C#是一种非常实用的编程语言,在进行网络爬虫方面也有很好的表现。在这篇文章中C#自动登录、浏览网页,轻松抓取数据!C#自动登录、浏览网页,轻松抓取数据!,我们将会探讨如何使用C#自动登录网页、浏览页面并抓取数据。

  方面一:准备工作

  在开始编写程序之前,我们需要准备好所需的工具和环境。首先,需要安装Visual Studio,并创建一个新的C#项目。其次C#自动登录网页浏览页面 抓取数据,需要安装HtmlAgilityPack和Newtonsoft.Json这两个NuGet包。

  方面二:获取cookie

  在自动登录网页之前,我们需要获取网站的cookie。我们可以通过向该网站发送POST请求来实现自动登录,并获取cookie。代码示例如下:

  

string url ="https://example.com/login";

string data ="username=yourusername&password=yourpassword";

HttpWebRequest request =(HttpWebRequest)WebRequest.Create(url);

request.Method ="POST";

request.ContentType ="application/x-www-form-urlencoded";

byte[] bytes = Encoding.UTF8.GetBytes(data);

request.ContentLength = bytes.Length;

Stream stream = request.GetRequestStream();

stream.Write(bytes,0, bytes.Length);

stream.Close();

WebResponse response = request.GetResponse();

string cookieHeader = response.Headers["Set-Cookie"];

  方面三:使用cookie访问网页

  获取cookie之后,我们就可以使用它来访问需要登录才能访问的网页了。代码示例如下:

  

string url ="https://example.com/protected-page";

HttpWebRequest request =(HttpWebRequest)WebRequest.Create(url);

request.Method ="GET";

request.Headers["Cookie"]= cookieHeader;

WebResponse response = request.GetResponse();

Stream stream = response.GetResponseStream();

StreamReader reader = new StreamReader(stream, Encoding.UTF8);

string content = reader.ReadToEnd();

  方面四:解析HTML

  获取到网页内容后,我们需要解析HTMLC#自动登录网页浏览页面 抓取数据,提取所需数据。我们可以使用HtmlAgilityPack这个第三方库来实现。代码示例如下:

  

HtmlDocument doc = new HtmlDocument();

doc.LoadHtml(content);

HtmlNodeCollection nodes = doc.DocumentNode.SelectNodes("//div[@class='item']");

foreach (HtmlNode node in nodes)

{

string title = node.SelectSingleNode("h3/a").InnerText;

string link = node.SelectSingleNode("h3/a").GetAttributeValue("href","");

string description = node.SelectSingleNode("p").InnerText;

}

  方面五:使用正则表达式

  除了使用HtmlAgilityPack外,我们还可以使用正则表达式来解析HTML。代码示例如下:

  

MatchCollection matches = Regex.Matches(content,"<div class=\"item\">.*?<h3><a href=\"(.*?)\".*?>(.*?)</a></h3>.*?<p>(.*?)</p>", RegexOptions.Singleline);

foreach (Match match in matches)

{

string link = match.Groups[1].Value;

string title = match.Groups[2].Value;

string description = match.Groups[3].Value;

}

  

  方面六:使用Json.NET解析JSON

  有些网站返回的数据是JSON格式的,我们可以使用Json.NET这个第三方库来解析JSON。代码示例如下:

  

string json =@"{""name"":""John Smith"",""age"":30,""city"":""New York""}";

JObject obj = JObject.Parse(json);

string name =(string)obj["name"];

int age =(int)obj["age"];

string city =(string)obj["city"];

  方面七:使用HttpClient

  在C#4.5及以上版本中,我们可以使用HttpClient这个类来发送HTTP请求。代码示例如下:

  

using (HttpClient client = new HttpClient())

{

HttpResponseMessage response = await client.GetAsync("https://example.com/protected-page");

string content = await response.Content.ReadAsStringAsync();

}

  方面八:使用WebBrowser控件

  如果我们需要模拟用户在浏览器中访问网页,我们可以使用WebBrowser控件来实现。代码示例如下:

  

WebBrowser browser = new WebBrowser();

browser.Navigate("https://example.com/protected-page");

while (browser.ReadyState != WebBrowserReadyState.Complete)

{

Application.DoEvents();

}

HtmlElementCollection elements = browser.Document.GetElementsByTagName("div");

foreach (HtmlElement element in elements)

{

if (element.GetAttribute("class")=="item")

{

string title = element.Children[0].Children[0].InnerText;

string link = element.Children[0].Children[0].GetAttribute("href");

string description = element.Children[1].InnerText;

}

}

browser.Dispose();

  方面九:使用PhantomJS

  如果我们需要在后台执行JS脚本,我们可以使用PhantomJS这个第三方库来实现。代码示例如下:

  

ProcessStartInfo startInfo = new ProcessStartInfo();

startInfo.FileName =@"C:\phantomjs\bin\phantomjs.exe";

startInfo.Arguments =@"C:\script.js";

startInfo.UseShellExecute = false;

startInfo.RedirectStandardOutput = true;

using (Process process = Process.Start(startInfo))

{

string output = process.StandardOutput.ReadToEnd();

}

  方面十:总结

  通过本文的讲解,我们了解了如何使用C#自动登录网页、浏览页面并抓取数据。无论是使用HtmlAgilityPack、正则表达式、Json.NET还是HttpClient、WebBrowser控件、PhantomJS,都有其特定的应用场景和优势。希望本文对你有所帮助。

  最后,推荐一款SEO优化工具——优采云(www.ucaiyun.com),让你的网站更容易被搜索引擎收录,提高排名。

0 个评论

要回复文章请先登录注册


官方客服QQ群

微信人工客服

QQ人工客服


线