C # Crawling images with AngleSharp,

Source: Internet
Author: User

C # Crawling images with AngleSharp,
AngleSharp Introduction

AngleSharp is a DLL component specially developed based on. NET (C #) to parse HTML source code. According to the html dom structure, the entire DOM has been transferred to the logical class structure. This structure can better operate DOM elements.

AngleSharp also provides some very useful extension methods, which are similar to jQuery and JavaScript. You can use the namespace AngleSharp to access methods such as Html, Css, Attr, and Text. These methods run on the specified IEnumerable <IElement> like the given IHtmlCollection. The objective is simple: easily modify a given DOM element. The biggest advantage of this plug-in: Support for outputJavascript,LinqSyntax,IDAndClassSelector and dynamically add nodes.

AngleSharp is an open-source project with the home address.

Reference

Http://www.cnblogs.com/pandait/p/AngleSharp.html

Https://www.cnblogs.com/liguobao/p/6130121.html

Get AngleSharp through NuGet

The easiest way to integrate AngleSharp into your project is to use NuGet. You can open the Software Package Manager Console (PM) and enter the following statement to install AngleSharp:

Install-Package AngleSharp

You can also use the graphics library Package Manager ("manage NuGet packages for solutions "). Search for "AngleSharp" in the official NuGet online source to find this library.

DOM operation example
// Create a (reusable) parser front-end var parser = new HtmlParser (); // html DOM node var source = "

Effect display

Change tag attributes

Add custom attributes to the <li> label

Var parser = new HtmlParser (); // generate HTML DOMvar document = parser for the following source code. parse ("<ul> <li> First element <li> Second element <li> third <li class = bla> Last "); // obtain all li elements and set the test attribute to a value to test var elements = document. querySelectorAll ("li "). attr ("test", "test"); // The element still contains all the li elements ViewData ["html"] = document. documentElement. outerHtml;

Effect display

Crawling watercress beauty pictures

Create a new Belle class to save the obtained Image Information

/// <Summary> /// parse html /// </summary> public class Belle {// <summary> /// title /// </summary> public string Title {get; set ;}//< summary> /// image address /// </summary> public string ImageUrl {get; set ;}}

GET html and parse

// Configure the Configuration to support document loading var config = Configuration. Default. withdefaloader loader (); // address var address = "https://www.dbmeinv.com/dbgroup/show.htm? Cid = 4 "; // request var var document = BrowsingContext. new (config ). openAsync (address); // obtain the html element var cells = document based on the class. result. querySelectorAll (". panel-body li "); // We are only interested in the text-select it with LINQList <Belle> list = new List <Belle> (); foreach (var item in cells) {var belle = new Belle
{Title = item. querySelector ("img "). getAttribute ("title"), ImageUrl = item. querySelector ("img "). getAttribute ("src")}; list. add (belle );
}
ViewData ["html"] = list;

The effect is as follows:

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.