上一篇文章介绍了,通过rss抓取新闻正文。这里介绍同时抓取多个RSS站点的正文,以及抓取正文中的图片。
我的RSS不是抓取 站点 <body></body>中的内容,而是需要的正文,广告评论等都排除在外。
第一部分:同时抓取多个站点,看看我的站点配置
<?xml version="1.0" encoding="GB2312"?>
<websites>
<site>
<name>IT之家</name>
<url>http://www.ithome.com/rss/</url>
<startTag><![CDATA[<div class="post_content" id="paragraph">]]></startTag>
<endTag><![CDATA[<div class="share">]]></endTag>
<encoding>GB2312</encoding>
<open>true</open>
</site>
<site>
<name>虎嗅网</name>
<url>http://www.huxiu.com/rss/0.xml</url>
<startTag><![CDATA[<table cellpadding="0" cellspacing="0" class="neirong-box" >]]></startTag>
<endTag><![CDATA[</table>]]></endTag>
<encoding>UTF-8</encoding>
<open>true</open>
</site>
</website>
这两个站点就是我需要抓取的,url是rss地址 startTag,endTag 是正文开始和结束的位置,encoding是站点的编码格式,open 表示是否抓取该站点,如果不清晰 请看
http://blog.youkuaiyun.com/kissliux/article/details/14227057
需要抓取的站点准备好了,开始解析吧。使用dom4j,请引入相关jar 我习惯使用maven管理这些jar
<dependency>
<groupId>dom4j</groupId>
<artifactId>dom4j</artifactId>
<version> 1.6.1</version>
</dependency>
public class Website {