编写一个爬虫demo ,爬取XXX大学的所有新闻的标题。
<font color=''>这里是新闻标题所在位置啦</font>
main代码:
public static void main(String[] args) throws IOException {
URL url=new URL("你要爬的网址可贴在这儿");
BufferedReader in=new BufferedReader(
new InputStreamReader(url.openStream(),"UTF-8"));//UTF-8处理乱码
String str;
while((str=in.readLine())!=null){
Pattern pattern = Pattern.compile("<font color=''>(.+?)</font>"); //正则表达式
// 定义一个matcher用来做匹配
Matcher matcher = pattern.matcher(str);
// 如果找到了
if (matcher.find()) {
// 打印出结果
System.out.println(matcher.group(1));
}
}
in.close();//关闭处理流
}
解析:
pattern 和matcher 的知识点
http://www.cnblogs.com/playing/archive/2011/03/15/1984943.html
附:
抓百度网页的图标代码
http://www.jb51.net/article/57193.htm