基于Source和正则表达式的Scala网页内容抓取
2013-02-26 11:16
856 查看
初学Scala,写个简单的抓取作为练手。
网页内容的抓取使用了Scala标准库的Source,网页内容的提取使用了正则表达式。中间练习了集合类的一些操作和文件读写操作,对正则表达式也重温了一下,特别是跨行匹配(多行匹配)研究了一些时间。提取后的文本写在了文件中。在Ubuntu 10.04 和 Scala 2.10.0 下运行成功。
代码如下:
网页内容的抓取使用了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()
相关文章推荐
- 基于Source和正则表达式的Scala网页内容抓取
- 基于Source和正则表达式的Scala网页内容抓取
- 基于Source和正则表达式的Scala网页内容抓取
- C# 正则表达式抓取网页上某个标签的内容,并替换链接地址和图片地址
- ObjC利用正则表达式抓取网页内容(网络爬虫)
- ObjC利用正则表达式抓取网页内容(网络爬虫)
- p2p nat打洞 C++抓取网页内容 抓取网页内超链接 正则表达式入门
- ObjC利用正则表达式抓取网页内容(网络爬虫)
- ObjC利用正则表达式抓取网页内容(网络爬虫)
- 用正则表达式抓取制定网页的特定内容(本文以抓取a标签为例)
- ObjC利用正则表达式抓取网页内容(网络爬虫)
- ObjC利用正则表达式抓取网页内容(网络爬虫)
- android 开发--抓取网页解析网页内容的若干方法(网络爬虫)(正则表达式)
- ObjC利用正则表达式抓取网页内容(网络爬虫)
- 转载自android 开发--抓取网页解析网页内容的若干方法(网络爬虫)(正则表达式)
- ObjC利用正则表达式抓取网页内容
- c# 正则表达式对网页进行内容抓取
- (转)ObjC利用正则表达式抓取网页内容(网络爬虫)
- PHP 正则表达式抓取网页内容。
- PHP 正则表达式抓取网页内容。