基于Source和正则表达式的Scala网页内容抓取


网页内容的抓取使用了Scala标准库的Source,网页内容的提取使用了正则表达式。中间练习了集合类的一些操作和文件读写操作,对正则表达式也重温了一下,特别是跨行匹配(多行匹配)研究了一些时间。提取后的文本写在了文件中。在Ubuntu 10.04 和 Scala 2.10.0 下运行成功。

代码如下:

import java.io._
 
def getIndex() = {
     val indexSource = scala.io.Source.fromURL( "http://www.yifan100.com/dir/15136/" ).mkString
     val indexRegex = "" "<a target=" _blank " href=" (.+\.html) " title=" .+ " >(.+)</a>" "" .r
     (List[(String, String)]() /: indexRegex.findAllMatchIn(indexSource).toList) { (result, item) =>
         ( "http://www.yifan100.com" + (item group 1 ), item group 2 ) :: result
     } // return List[(url:String, title:String)]
}
 
def getContent(url:String) = {
     val raw = scala.io.Source.fromURL(url).mkString
     val reg = "" "(?s).*<div class=" artcontent ">(.*)<div id=" zhanwei ">.*" "" .r
     (reg findFirstMatchIn raw).map[String](item => {
             val s = (item group 1 ).replaceAll( "<br>" , "\r\n" ).
                 replaceAll( "" "(?s)</?.*?>" "" , "" ).
                 replaceAll( "" "^\s+" "" , "" ).
                 replaceAll( " " , " " )
             s
         }
     ) // return Option[String]
}
 
def writeContent(content:Option[String], title:String) {
     if (content.isEmpty)
         println( "Not write " + title + ".txt" )
     else {
         val writer = new PrintWriter( new File(title + ".txt" ))
         writer write content.get
         writer.close()
         println( "Write " + title + ".txt" )
     }
}
 
def getIt() {
     getIndex().foreach(item => writeContent(getContent(item._1), item._2))
}
 
getIt()
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值