티스토리 뷰

공부

[Jsoup] GET & POST crawling

승가비 2022. 9. 10. 10:26
728x90

Example

    private fun token(email: String, password: String): String {
        val url = "https://asdf.com/tokens"

        val headers = mapOf(
            "Accept" to "application/json",
            "Content-Type" to "application/json",
            "Authorization" to "null"
        )
        val json = mapOf(
            "auth_type" to "CREDENTIAL",
            "credential_type_payload" to mapOf(
                "email" to email,
                "password" to password,
            ),
        ).toJson()

        return CrawlUtil.post(url, headers, json)["token"] as String
    }

CrawlUtil

import org.jsoup.Connection
import org.jsoup.Jsoup

object CrawlUtil {
    private const val TIMEOUT = 5 * 60 * 1000
    private const val MAX_BODY_SIZE = 0
    private const val IGNORE_CONTENT_TYPE = true

    private fun connection(
        url: String,
        headers: Map<String, String>,
        json: String,
        data: Map<String, String>? = emptyMap()
    ): Connection {
        var connection = Jsoup.connect(url)

        headers.forEach {
            connection = connection.header(it.key, it.value)
        }

        data!!.forEach {
            connection = connection.data(it.key, it.value)
        }

        return connection
            .timeout(TIMEOUT)
            .maxBodySize(MAX_BODY_SIZE)
            .ignoreContentType(IGNORE_CONTENT_TYPE)
            .requestBody(json)
    }

    fun get(
        url: String,
        headers: Map<String, String>,
        json: String,
        data: Map<String, String>? = emptyMap()
    ): Map<String, Any> {
        return connection(url, headers, json, data)
            .get()
            .text()
            .toObject()
    }

    fun post(
        url: String,
        headers: Map<String, String>,
        json: String,
        data: Map<String, String>? = emptyMap()
    ): Map<String, Any> {
        return connection(url, headers, json, data)
            .post()
            .text()
            .toObject()
    }
}

ExtensionUtil

inline fun <reified T> String.toObject(): T {
    return JacksonUtil.toObject(this)
}

JacksonUtil

import com.fasterxml.jackson.core.type.TypeReference
import com.fasterxml.jackson.databind.DeserializationFeature
import com.fasterxml.jackson.databind.ObjectMapper
import com.fasterxml.jackson.databind.SerializationFeature
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule
import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateDeserializer
import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateTimeDeserializer
import com.fasterxml.jackson.module.afterburner.AfterburnerModule
import com.fasterxml.jackson.module.kotlin.KotlinModule
import java.time.LocalDate
import java.time.LocalDateTime
import java.time.format.DateTimeFormatter

object JacksonUtil {
    val objectMapper: ObjectMapper = ObjectMapper()
        .registerModule(KotlinModule.Builder().build())
        .registerModule(JavaTimeModule())
        .registerModule(AfterburnerModule())
        .configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false)
        .configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false)
        .configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false)
        .registerModule(
            JavaTimeModule().apply {
                addDeserializer(LocalDate::class.java, LocalDateDeserializer(DateTimeFormatter.ISO_DATE))
                addDeserializer(
                    LocalDateTime::class.java,
                    LocalDateTimeDeserializer(DateTimeFormatter.ISO_DATE_TIME)
                )
            }
        )

    fun toJson(obj: Any?): String {
        if (obj == null) {
            return ""
        }

        return objectMapper.writeValueAsString(obj)
    }

    fun merged(a: MutableMap<String, Any>, b: Any): Map<String, Any> {
        return objectMapper
            .readerForUpdating(a)
            .readValue(toJson(b))
    }

    inline fun <reified T> toObject(s: String): T {
        return objectMapper.readValue(s, object : TypeReference<T>() {})
    }
}

https://stackoverflow.com/questions/27463036/jsoup-http-post-with-payload

 

Jsoup HTTP POST with payload

I am trying to make this HTTP request via jsoup as given here: http://api.decarta.com/v1/[KEY]/batch?requestType=geocode And here is my code for that: String postUrl = postURLPrefix + apiKey + &qu...

stackoverflow.com

https://partnerjun.tistory.com/43

 

Java HTML parser, Jsoup로 원하는 값 얻어내기 - 로그인

이 포스트에서는 로그인이 필요한 사이트와 Request Header를 검사하는 사이트를 파싱하는 과정을 적어둔다. 0. 웹 사이트 로그인 먼저 웹 사이트에 로그인에 대해 다시 생각해 볼 필요가 있다. 최

partnerjun.tistory.com

https://stackoverflow.com/questions/10012399/how-to-post-data-into-website-using-jsoup

 

How to POST Data into website using Jsoup

I am trying to POST data into website to make a login into the site using Jsoup , but its not working ? I am trying the code Document docs = Jsoup.connect("http://some.com/login") .d...

stackoverflow.com

https://dirask.com/posts/Java-Jsoup-FIX-for-exception-com-fasterxml-jackson-core-io-JsonEOFException-Unexpected-end-of-input-in-field-name-at-Source-java-io-StringReader-1725dc0f-line-1-column-1046939-kDLGL1

 

Java Jsoup - FIX for exception com.fasterxml.jackson.core.io.JsonEOFException: Unexpected end-of-input in field name at [Source:

1. Problem description Code reproduction to get exception, json from my spring controller backend is quite big about 30 MB With above code I get this exception:...

dirask.com

https://yj-something.tistory.com/2

 

JAVA 웹 크롤링(Web crawling)

준비 우선 java러 웹 크롤링을 하기 위해서는 jsoup이라는 라이브러리가 필요하다. 물론 jsoup이 없어도 크롤링을 할 수 있지만 라이브러리를 사용하는 것이 더 편리하기에 라이브러리를 사용하여 J

yj-something.tistory.com

 

728x90
댓글