This page is no longer maintained — Please continue to the home page at www.scala-lang.org

Re: More elegant way of reading HTML from a URL than this?

1 reply
Stepan Koltsov 2
Joined: 2009-01-21,
User offline. Last seen 42 years 45 weeks ago.

On Wed, Jan 21, 2009 at 19:42, Seth Tisue wrote:
>>>>>> "Kenneth" == Kenneth McDonald writes:
>
> Kenneth> Here's a bit of code I wrote to read the HTML from a URL, and
> Kenneth> return it as a string. I was wondering if a Scala guru could
> Kenneth> show me the "right" way to do this. I'm sure there's a more
> Kenneth> elegant solution.
>
> def read(url:String):String =
> io.Source.fromInputStream(new java.net.URL(url).openStream()).mkString
> println(read("http://www.yahoo.com/"))

InputStream is left open?

scala.io.Source must die.

S.

Stepan Koltsov
Joined: 2008-12-20,
User offline. Last seen 42 years 45 weeks ago.
Re: More elegant way of reading HTML from a URL than this?

On Wed, Jan 21, 2009 at 19:43, Derek Chen-Becker wrote:
> Stepan Koltsov wrote:
>> InputStreamResource.url("http://...").readString
>>
>> InputStreamResource.url("http://...").readLines
>>
>> InputStreamResource.url("http://...").lines.foreach(println(_))
>>
>> InputStreamResource is part of scalax.
>>
>> BTW, I think InputStreamResource-like classes must be included into
>> the scala standard library.
>
> I second that. Scala's included IO package is pretty anemic. At the very
> least, it would be nice to have some wrappers similar to JCL to add some
> nice scala-ish functionality to existing Java IO classes. Of course, I
> don't personally have time to work on it so I can't complain too loudly ;)

Acutually, JDK has no InputStreamResource-like classes, and it is a problem too.

S.

Copyright © 2012 École Polytechnique Fédérale de Lausanne (EPFL), Lausanne, Switzerland