In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly explains "how to use C # to climb web page data". The content in the article is simple and clear, and it is easy to learn and understand. Please follow the editor's way of thinking to study and learn "how to climb web page data with C#"!
CSQuery
1. Installation
Address of github: https://github.com/zone117x/CsQuery and then nuget it in vs:
two。 Give me a few examples.
Everything is ready, so how to use it? Don't worry, let me give you two examples of blog park.
1) extract the friendship links from the home page to
As shown in the image above, if you want to get a few big words of the friendship link here, it is definitely impossible to use text () directly. By default, it will also capture the text of all the child nodes, as shown below:
What should I do with it? You can use the contents method provided by jquery, and then determine whether there is a text node among all the acquired child nodes, and finally get the content of the text node, as shown in the following code:
It is done with js, so how to do it with CSQuery code? To imitate, the code is as follows:
Static void Main (string [] args) {var jquery = CQ.CreateDocument (new WebClient () .DownloadString ("http://cnblogs.com")); var content = jquery [" # friend_link "] .Contents () .Filter ((dom) = > {return dom.NodeType = = NodeType.TEXT_NODE;}) .Text (); Console.WriteLine (content) }
I don't know if it's troublesome to extract such content with xpath, but it's not easy to use jquery, but you're familiar with it.
2) how to color some elements in html
Sometimes you need to change the color of some html tags for business, such as changing the blog question and zone in the tabmenu on the home page to red, as shown below:
How to deal with it with CSQuery? If you have played jquery, the general steps are as follows:
Use each to iterate through each child li tag
Use the CSS method to style the a tag in li
Generate a new html using Render rendering
With the steps, the C# code is as follows:
Static void Main (string [] args) {Config.HtmlEncoder = HtmlEncoders.None; var jquery = CQ.CreateDocument (new WebClient () .DownloadString ("http://cnblogs.com")); var html = jquery [" # nav_left li "] .Each (dom = > {var self = jquery [dom]) Var text = self.Text (); if (text = = "blog question" | | text = = "zone") {self.Find ("a") .CssSet (new {color = "red"});}) .Render ();}
3) other operation methods
In addition to the above two operating methods, you can also use after,before,replaceAll,IS and so on more than a hundred practical methods, this article certainly can not be introduced one by one, you can download if you are interested to have a look, make trouble.
Other uses
In addition to grabbing the elements in html, I think it can also be used to manipulate email templates when sending emails. after all, a long time ago, people used jquery to draw html, so it is also possible to use CSQuery. There are advantages and disadvantages to using xslt. Let's give an example:
1. Generate a html template
two。 Append li to ul using CSQuery
You can use Append to append content to
Class Program {static void Main (string [] args) {Config.HtmlEncoder = HtmlEncoders.None; var strlist = new string [2] {"1", "2"}; var path = Environment.CurrentDirectory + "\\ 2.html"; var jquery = CQ.CreateFromFile (path) Foreach (var str in strlist) {jquery.Find ("# main") .Append ($"{str}");} var html = jquery.Render ();}}
3. Partially rendered RenderSelection
The Render method is to render the entire Dom to html, but sometimes you only need to get the modified part of the content instead of the entire html. This involves partial rendering. You can use the RenderSelection method. The code is as follows:
Static void Main (string [] args) {Config.HtmlEncoder = HtmlEncoders.None; var strlist = new string [2] {"1", "2"}; var path = Environment.CurrentDirectory + "\\ 2.html"; var jquery = CQ.CreateFromFile (path); var current = jquery.Find ("# main") Foreach (var str in strlist) {current.Append ($"{str}");} var html = current.RenderSelection (); Console.WriteLine (html) }-output-12 Thank you for your reading, the above is the content of "how to use C# to climb web data". After the study of this article, I believe you have a deeper understanding of how to use C# to climb web data, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
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.