0
object extract_data {

  def main(args: Array[String]): Unit = {
    var url = "https://www.ebay.com/itm/SORIN-GRIM-NEMESIS-Shadows-Over-Innistrad-SOI-Planeswalker-Magic-MTG-MINT-CARD/311569519215"
    val x: OfferInformation = extractOffer(url)
    println(x)
  }

  def extractOffer(url: String): OfferInformation = {
    val str: InputStream = new URL(url).openStream()

    var input: String = Source.fromInputStream(str).mkString
    val doc: Document = Jsoup.parse(input, "UTF-8")

    var sellerName = doc.select(".mbg-nw").text()
    //println(sellerName)

    var table = doc.select(".itemAttr table").get(0)
    //println (table)
    val rows = table.select("tr")
    // from here
    var i: Int = 0
    var size: Int = rows.size()
    var edition: String = ""
    var cond: String = ""
    var lang: String = ""

    for (i <- 0 to size) {
      val row = rows.get(i)
      val cols = row.select("td")

      for (i <- 0 to 3) {
        val columnValue=cols.get(i).text

        columnValue match {

          case "Edition:" =>
            edition = cols.get(i + 1).text
            //println(edition)
          case other => other
        }

        columnValue match {

          case "Condition:" =>
            cond = cols.get(i + 1).text
            //println(cond)
          case other => other
        }

        columnValue match {

          case "Language:" =>
            lang = cols.get(i + 1).text
            //println(lang)
          case other => other
        }

      }

    }

    // till here
    return OfferInformation("Not Yet Taken", edition, cond, lang, sellerName)
  }

}

case class OfferInformation(cardName: String, edition: String, cond: String, lang: String, sellerName: String)

I am crawling the eBay page for practice using scala but when I am returning OfferInformation, I am getting the error "Exception in thread "main" java.lang.IndexOutOfBoundsException: Index 2 out-of-bounds for length 2". Can you please help me to fix the problem and also suggest how to make the code better ?

0 Answers0