GithubHelp home page GithubHelp logo

kiyeonk / ksoup Goto Github PK

View Code? Open in Web Editor NEW

This project forked from fleeksoft/ksoup

0.0 0.0 0.0 754 KB

Ksoup is a Kotlin Multiplatform library for working with HTML and XML. It's a port of the renowned Java library, jsoup.

Home Page: https://fleeksoft.github.io/ksoup/

License: Apache License 2.0

Kotlin 80.08% HTML 19.92%

ksoup's Introduction

Ksoup: Kotlin Multiplatform HTML & XML Parser

Ksoup is a Kotlin Multiplatform library for working with real-world HTML and XML. It's a port of the renowned Java library, jsoup, and offers an easy-to-use API for URL fetching, data parsing, extraction, and manipulation using DOM and CSS selectors.

Kotlin Maven Central

badge-android badge-ios badge-jvm badge-linux badge-nodejs

Ksoup implements the WHATWG HTML5 specification, parsing HTML to the same DOM as modern browsers do, but with support for Android, JVM, and native platforms.

Features

  • Scrape and parse HTML from a URL, file, or string
  • Find and extract data using DOM traversal or CSS selectors
  • Manipulate HTML elements, attributes, and text
  • Clean user-submitted content against a safe-list to prevent XSS attacks
  • Output tidy HTML

Ksoup is adept at handling all varieties of HTML found in the wild.

Getting started

Ksoup is published on Maven Central

Include the dependency in commonMain. Latest version Maven Central

commonMain.dependencies {
    implementation("com.fleeksoft.ksoup:ksoup:<version>")

// Optional: Include only if you need to use network request functions such as
// Ksoup.parseGetRequest, Ksoup.parseSubmitRequest, and Ksoup.parsePostRequest
    implementation("com.fleeksoft.ksoup:ksoup-network:<version>")
}

Parsing HTML from a String with Ksoup

val html = "<html><head><title>One</title></head><body>Two</body></html>"
val doc: Document = Ksoup.parse(html = html)

println("title => ${doc.title()}") // One
println("bodyText => ${doc.body().text()}") // Two

This snippet demonstrates how to use Ksoup.parse for parsing an HTML string and extracting the title and body text.

Fetching and Parsing HTML from a URL using Ksoup

//Please note that the com.fleeksoft.ksoup:ksoup-network library is required for Ksoup.parseGetRequest.
val doc: Document = Ksoup.parseGetRequest(url = "https://en.wikipedia.org/") // suspend function
// or
val doc: Document = Ksoup.parseGetRequestBlocking(url = "https://en.wikipedia.org/")

println("title: ${doc.title()}")
val headlines: Elements = doc.select("#mp-itn b a")

headlines.forEach { headline: Element ->
    val headlineTitle = headline.attr("title")
    val headlineLink = headline.absUrl("href")

    println("$headlineTitle => $headlineLink")
}

In this example, Ksoup.parseGetRequest fetches and parses HTML content from Wikipedia, extracting and printing news headlines and their corresponding links.

For further documentation, please check here: Ksoup

Open source

Ksoup is an open source project, a Kotlin Multiplatform port of jsoup, distributed under the Apache License, Version 2.0. The source code of Ksoup is available on GitHub.

Development and Support

For questions, ideas, or contributions regarding Ksoup, please contact us via email or create new pull requests.

Report any issues on our GitHub page, ensuring to check for duplicates beforehand.

Library Status

Platform Status Notes
Android Beta
JVM Beta
iOS Alpha Does not support few charsets.
Linux Experimental Does not support gzip files and few charsets.
JS Alpha Does not support few charsets.
Native MacOS Coming Soon
Native Windows Coming Soon

Licence

Copyright 2023 Sabeeh Ul Hussnain

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

   http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

ksoup's People

Contributors

itboy87 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.