Extract the date from the "#date" elementĭoc > extractor( "#date ", text, asLocalDate( "yyyy-MM-dd ")) The > and >?> operators shown above accept an HtmlExtractor as their right argument, a trait with a very simple interface: More information about each browser and its semantics can be obtained in the Scaladoc of each implementation. ![]() It supports several compatibility modes, allowing it to emulate browsers such as Internet Explorer.ĭue to its speed and maturity, JsoupBrowser is the recommended browser to use when JavaScript execution is not needed. HtmlUnitBrowser simulates thoroughly a web browser, executing JavaScript code in the pages in addition to parsing HTML. HtmlUnitBrowser is based on HtmlUnit, a GUI-less browser for Java programs.As such, it is limited to working strictly with the HTML sent in the page source JsoupBrowser provides powerful and efficient document querying, but it doesn't run JavaScript in the pages. JsoupBrowser is backed by jsoup, a Java HTML parser library.The library currently provides two built-in implementations of Browser: Depending on the browser used, Document and Element instances may have different semantics, mainly on their immutability guarantees. Most notably, they implement get, post, parseFile and parseString methods for retrieving documents from different sources. The library represents HTML documents and their elements by Document and Element objects, simple interfaces containing methods for retrieving information and navigating through the DOM.īrowser implementations are the entrypoints for obtaining Document instances. They are followed by a description of the full capabilities of the DSL, including the ability to parse content after extracting, validating the contents of a page and defining custom extractors or validators. In the next two sections the core classes used by this library are presented. Follow that link and print both the article title and its short // description (inside ".lead") for println( "= " headline.text " = \n " headlineDesc) ![]() Go to a news website and extract the hyperlink inside the h1 element if it // exists. If the element may or may not be in the page, the >?> tries to extract the content and returns it wrapped in an Option: res2: String = "width=device-width, initial-scale=1" res1: List = List("Home", "Section 1", "", "Section 3") // From the meta element with "viewport" as its attribute name, extract the // text in the content attributeĭoc > attr( "content ")( "meta ") items: List = List( // JsoupElement(Home), // JsoupElement(Section 1), // JsoupElement(Section 2), // JsoupElement(Section 3) // ) // From each item, extract all the text inside their elements res0: String = "Test page h1" // Extract the elements inside #menu val items = doc > elementList( "#menu span ") _ // Extract the text inside the element with id "header"
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |