本文整理汇总了Scala中org.jsoup.select.Elements类的典型用法代码示例。如果您正苦于以下问题:Scala Elements类的具体用法?Scala Elements怎么用?Scala Elements使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了Elements类的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Scala代码示例。
示例1: HtmlLifter
//设置package包名称以及导入依赖的类
package com.twitter.diffy.lifter
import org.jsoup.Jsoup
import org.jsoup.nodes.{Document, Element}
import org.jsoup.select.Elements
import scala.collection.JavaConversions._
object HtmlLifter {
def lift(node: Element): FieldMap[Any] = node match {
case doc: Document =>
FieldMap(
Map(
"head" -> lift(doc.head),
"body" -> lift(doc.body)
)
)
case doc: Element => {
val children: Elements = doc.children
val attributes =
FieldMap[String](
doc.attributes.asList map { attribute =>
attribute.getKey -> attribute.getValue
} toMap
)
FieldMap(
Map(
"tag" -> doc.tagName,
"text" -> doc.ownText,
"attributes" -> attributes,
"children" -> children.map(element => lift(element))
)
)
}
}
def decode(html: String): Document = Jsoup.parse(html)
}
示例2: JsoupHelper
//设置package包名称以及导入依赖的类
package util
import java.util.stream.Collectors
import org.jsoup.nodes.Element
import org.jsoup.select.Elements
object JsoupHelper {
implicit def elementsToElements4Scala(elements: Elements): Elements4Scala = {
new Elements4Scala(elements)
}
}
class Elements4Scala(that: Elements) {
def toElementArray: Array[Element] = {
val list: java.util.List[Element] = that.stream.collect(Collectors.toList[Element])
list.toArray(new Array[Element](list.size()))
}
}
示例3: TorrentSearchResult
//设置package包名称以及导入依赖的类
package providers
import org.jsoup.Jsoup
import org.jsoup.nodes.Element
import org.jsoup.select.Elements
import play.api.libs.json.Json
import play.api.libs.ws.WSClient
import scala.collection.JavaConversions._
import scala.concurrent.ExecutionContext.Implicits.global
case class TorrentSearchResult(name:String,magnetLink:String,
size:String,age:String,
seeders:String, leechers:String,
provider:String) extends SearchResult
object TorrentSearchResult{
implicit val torrentSearchResultWrites = Json.writes[TorrentSearchResult]
}
class KatcrProvider extends SearchProvider {
override def search(searchString:String, ws: WSClient, numberofResulstToReturn:Int) = {
val searchProviderURL = "https://kat.cr/usearch/"
ws.url(searchProviderURL + searchString).get().map {
response => { //process the response
extractTopTorrents(response.body,numberofResulstToReturn)
}
}
}
def processKatcrTableRow(tds: Elements):TorrentSearchResult =
TorrentSearchResult(tds(0).text,
tds.select("a[title=\"Torrent magnet link\"]").first().attr("href"),
tds(1).text,
tds(2).text,tds(3).text,tds(4).text, "kat.cr")
def extractTopTorrents(htmlString:String, numberOfResults:Int) = {
val tables:Elements = Jsoup.parse(htmlString).select("table")
val table = tables.get(1)
// Get the top numberOfResults and the return a list of TorrentSearchResult..
table.select("tr").slice(1,numberOfResults+1).foldLeft(List[TorrentSearchResult]()){
(result,tr) => // process each tr element and prepend to result
processKatcrTableRow(tr.select("td")) :: result
}
}.reverse //since the list is sorted in the reverse seeders order
.asInstanceOf[List[SearchResult]]
}